[Solved] How can I open the same file twice? not the same behaviour as on windows..
Posted: Thu Nov 07, 2019 5:39 pm
How can I open the same file once to write a log continuously, and second to read it and upload it through wifi.
I wish to have 2 cursors moving on the file, and not have to fseek back and forth to impelement this feature.
I tested this code with esp-idf-v3.3 and on windows with CodeBlocks
codeblocks works as expected, but with esp-idf with sdcard using FAT the second fopen call returns NULL.
Is there any flag I have to set or compiler option to make this code work? I've tried with w+ and r+ but got the same result.
I wish to have 2 cursors moving on the file, and not have to fseek back and forth to impelement this feature.
I tested this code with esp-idf-v3.3 and on windows with CodeBlocks
Code: Select all
int main(int argc, char *argv[])
{
printf("open\n");
FILE *w = fopen("logtest.txt", "w");
printf("read open = %x\n", w);
if(!w)
return 0;
fprintf(w, "aaaa");
fflush(w);
//fsync(fileno(w));
FILE *r = fopen("logtest.txt", "r");
printf("read open = %x\n", r);
if(!r)
return 0;
printf(".%c", fgetc(r));
printf(".%c", fgetc(r));
printf(".%c\n", fgetc(r));
fprintf(w, "bbbb");
fflush(w);
//fsync(fileno(w));
printf(".%c", fgetc(r));
printf(".%c", fgetc(r));
printf(".%c", fgetc(r));
printf(".%c", fgetc(r));
printf(".%c", fgetc(r));
printf(".%c", fgetc(r));
return 1;
}
Is there any flag I have to set or compiler option to make this code work? I've tried with w+ and r+ but got the same result.