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

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

[Solved] 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.
Last edited by gunar.kroeger on Wed Nov 20, 2019 4:58 pm, edited 1 time in total.
"Running was invented in 1612 by Thomas Running when he tried to walk twice at the same time."

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

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

Postby gunar.kroeger » Mon Nov 18, 2019 2:14 pm

should I report this as a bug?
"Running was invented in 1612 by Thomas Running when he tried to walk twice at the same time."

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

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

Postby ESP_Angus » Tue Nov 19, 2019 11:09 pm

Hi gunar,

This is a limitation of the FatFs library that we use. Set this config option to 2 and you should be able to have the same file open twice:

https://docs.espressif.com/projects/esp ... fs-fs-lock

Note that this comes at a tradeoff of memory usage and efficiency (still relatively low cost compared to ESP32's available resources, though.)

Angus

Who is online

Users browsing this forum: No registered users and 213 guests