Hello. I am using SPIFFS for my esp32 using esp-idf. See the function below that opens the file inside my SPIFFS:
I would like to know why the following works without any issues:
And why this does not work:
I do not want to hardcode the name of the file. I want to pass the name of the file and then open it. I cannot understand why it does not work because I create exactly same string as if I hardcode it. The serial monitor:
Code:
static void read_hello_txt(char* file_name)
{
ESP_LOGI(TAG, "Reading hello.txt");
// Open for reading hello.txt
char file_location[30];
memset(file_location, 0, sizeof(file_location));
strcat(file_location,"/spiffs/");
strcat(file_location,file_name);
printf("file location = %s \n",file_location);
printf("size of string1 = %d \n ",strlen("/spiffs/hello.txt"));
printf("size of string2 = %d \n ",strlen(file_location));
//FILE* f = fopen("/spiffs/hello.txt", "r"); // works without any issues
FILE* f = fopen(file_name, "r"); // does not work
if (f == NULL) {
ESP_LOGE(TAG, "Failed to open hello.txt");
return;
}
char buf[64];
memset(buf, 0, sizeof(buf));
fread(buf, 1, sizeof(buf), f);
fclose(f);
// Display the read contents from the file
ESP_LOGI(TAG, "Read from hello.txt: %s", buf);
}
Code:
//FILE* f = fopen("/spiffs/hello.txt", "r");
Code:
char file_location[30];
memset(file_location, 0, sizeof(file_location));
strcat(file_location,"/spiffs/");
strcat(file_location,file_name);
FILE* f = fopen(file_name, "r");
Code:
file location = /spiffs/hello.txt
size of string1 = 17
size of string2 = 17
E (365) example: Failed to open hello.txt