Code: Select all
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);
FILE* f = fopen("/spiffs/hello.txt", "r");
//FILE* f = fopen(file_name, "r");
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: Select all
FILE* f = fopen("/spiffs/hello.txt", "r");
I have wrote some code that would create a string based on what I pass to the function
Code: Select all
char file_location[30]; // create temporary char array to store the final file location
memset(file_location, 0, sizeof(file_location)); // ensure that memory is clear
strcat(file_location,"/spiffs/"); // append /spiffs/ to the string
strcat(file_location,file_name); // append the name that is passed as a variable
printf("file location = %s \n",file_location);
FILE* f = fopen(file_name, "r");
Code: Select all
file location = /spiffs/hello.txt
E (365) example: Failed to open hello.txt
Code: Select all
FILE* f = fopen("/spiffs/hello.txt", "r");