I am having problems writing file errors on Windows. I simplified it to this example:
FILE* f = fopen("test.out", "r+b"); fseek(f, -1, SEEK_END); // one byte before the end printf("read byte: %c\n", fgetc(f)); // read the last byte; now at the end printf("attempting write: %d\n", fputs("text", f));
This correctly outputs the last byte of test.out , but fputs fails and returns -1. These similar examples work fine:
Do not read
FILE* f = fopen("test.out", "r+b"); fseek(f, 0, SEEK_END); // this is where I ended up after the fgetc() above printf("attempting write: %d\n", fputs("text", f));
Look to the end after reading (although we are already there)
FILE* f = fopen("test.out", "r+b"); fseek(f, -1, SEEK_END); printf("read byte: %c\n", fgetc(f)); fseek(f, 0, SEEK_END); printf("attempting write: %d\n", fputs("text", f));
Search for exactly where we are already
FILE* f = fopen("test.out", "r+b"); fseek(f, -1, SEEK_END); printf("read byte: %c\n", fgetc(f)); fseek(f, ftell(f), SEEK_SET); printf("attempting write: %d\n", fputs("text", f));
Read, but not the last byte
FILE* f = fopen("test.out", "r+b"); fseek(f, -2, SEEK_END); // two bytes before the end printf("read byte: %c\n", fgetc(f)); // read the penultimate byte printf("attempting write: %d\n", fputs("text", f));
Read the end (...)
FILE* f = fopen("test.out", "r+b"); fseek(f, -1, SEEK_END); // one byte before the end printf("read byte: %c\n", fgetc(f)); // read the last byte; now at the end printf("read byte: %c\n", fgetc(f)); // read a garbage byte printf("attempting write: %d\n", fputs("text", f));
All this indicates a stream error or an eof problem, but ferror(f) and feof(f) both return 0 until fputs() . After, ferror(f) is nonzero, but errno is 0, so I don't know what the problem is
I see this only on Windows, both in Visual Studio 2008 and in GCC 4.7.2 (MinGW). On Linux, the same code works without errors
source share