The following is my code:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char buf[128];
memset(buf, 0, sizeof(buf));
FILE *fp = fopen("prepatch.log", "wb+");
fwrite(buf, 1, sizeof(buf), fp);
fseek(fp, 0, SEEK_SET);
#if 1
fread(buf, 1, 8, fp);
#else
fseek(fp, 8, SEEK_CUR);
#endif
fwrite("test", 1, 4, fp);
fclose(fp);
return 0;
}
I cannot write "test" to the file if I read it.
But fseek does correct.
I find that 'FILE::_flag' is not set correctly.
So _flush does not flush the buffer.
VC2003 and VC2005 is same.
Doug Harrison [MVP] - 16 Aug 2006 16:08 GMT
>The following is my code:
>
[quoted text clipped - 28 lines]
>
>VC2003 and VC2005 is same.
You need the fseek between the read and the write for the reason given
here:
Stream States
http://msdn2.microsoft.com/en-us/library/kb1at4by.aspx
<q>
You cannot call a write function if the last operation on the stream was a
read, unless that read operation set the end-of-file indicator.
</q>

Signature
Doug Harrison
Visual C++ MVP