How can I open the same file twice? not the same behaviour as on windows..

User avatar
gunar.kroeger
Posts: 89
Joined: Fri Jul 27, 2018 6:48 pm

How can I open the same file twice? not the same behaviour as on windows..

Postby gunar.kroeger » 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

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;
    }
  
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.
"Running was invented in 1612 by Thomas Running when he tried to walk twice at the same time."

Who is online

Users browsing this forum: Google [Bot] and 20 guests