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);
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);
...
.