Using "r +" in fopen on windows vs linux

I played with some code that opened, read and modified a text file. A quick (simplified) example:

#include <stdio.h>
int main()
{
    FILE * fp = fopen("test.txt", "r+");
    char line[100] = {'\0'};
    int count = 0;
    int ret_code = 0;
    while(!feof(fp)){
        fgets(line, 100, fp);
        // do some processing on line...
        count++;
        if(count == 4) {
          ret_code = fprintf(fp, "replaced this line\n");
          printf("ret code was %d\n", ret_code);
          perror("Error was: ");
        }
    }
    fclose(fp);
    return 0;
}

Now on Linux compiled with gcc (4.6.2), this code runs and modifies the file with the 5th line. The same code running on Windows7, compiled with Visual C ++ 2010 starts and claims to be successful (reports a return code of 19 characters and perrorsays “No error”), but cannot replace the string.

On Linux, my file has full permissions:

-rw-rw-rw- 1 mike users 191 Feb 14 10:11 test.txt

And as far as I can say the same thing on Windows:

test.txt (right click) → properties → Security
"Allow" is checked for reading and writing for the user, system and administrator.

, MinGW gcc Windows, , Visual ++.

- , , "" r+ fopen() Windows?


:
, Microsoft , "r +" , :

"r +", "w +" "a +", ( "" ). fflush, fsetpos, fseek rewind. fsetpos fseek, .

, :

        ...
        if(count == 4) {
          fflush(fp);
          ret_code = fprintf(fp, "replaced this line\n");
          fflush(fp);
          printf("ret code was %d\n", ret_code);
          ...

.

+5
2

Linux fopen():

/ . , ANSI C , , . ( , , .) ( Linux) fseek (3) fgetpos (3) . - ( fseek (..., 0L, SEEK_CUR) .

, fseek() (, . fseek(..., 0, SEEK_CUR)) .

+3

fflush() - . - :

fseek(fp, ftell(fp), SEEK_SET); // not fflush(fp);

C99 (7.19.5.3/6 " fopen):

('+' ), , . fflush (fseek, fsetpos ), , .

+3

All Articles