How to access file on SPIFFS by fopen() on ESP32-Arduino?

TridentTD
Posts: 1
Joined: Wed Oct 02, 2019 9:05 am

How to access file on SPIFFS by fopen() on ESP32-Arduino?

Postby TridentTD » Wed Oct 02, 2019 9:18 am

On ESP-Arduino, there is SPIFFS instant for accessing to the files on SPIFFS,
however my propose wish to access by fopen() for low-level another c library.

I can test to write a file on SPIFFS by fopen() with the following code.

Code: Select all

#include "FS.h"
#include "SPIFFS.h"

void listDir(fs::FS &fs, const char * dirname, uint8_t levels=0);

void setup(){
  Serial.begin(115200);
  SPIFFS.begin();
  
  FILE* f = fopen("/spiffs/hello.txt", "w");
  if (f == NULL) {
      Serial.println( "Failed to open file for writing");
      return;
  }
  fprintf(f, "Hello World!\n");
  fclose(f);
  Serial.println("file created");
  
  listDir(SPIFFS, "/");  // from this result, it founds "hello.txt" on SPIFFS
} 

void loop(){
}

void listDir(fs::FS &fs, const char * dirname, uint8_t levels) {
  Serial.printf("Listing directory: %s\r\n", dirname);

  File root = fs.open(dirname);
  if (!root) {
    Serial.println("- failed to open directory");
    return;
  }
  if (!root.isDirectory()) {
    Serial.println(" - not a directory");
    return;
  }

  File file = root.openNextFile();
  while (file) {
    if (file.isDirectory()) {
      Serial.print("  DIR : ");
      Serial.println(file.name());
      if (levels) {
        listDir(fs, file.name(), levels - 1);
      }
    } else {
      Serial.print("  FILE: ");
      Serial.print(file.name());
      Serial.print("\tSIZE: ");
      Serial.println(file.size());
    }
    file = root.openNextFile();
  }
}

By the code, it founds "hello.txt" on SPIFFS.
however when I want to test reading the "hello.h" file. It can't read.

Code: Select all

// TEST for reading by fopen() on SPIFFS

#include "FS.h"
#include "SPIFFS.h"

void listDir(fs::FS &fs, const char * dirname, uint8_t levels=0);

void setup(){
  Serial.begin(115200);
  SPIFFS.begin();
  
  listDir(SPIFFS, "/");  // from this result, it founds "hello.txt" on SPIFFS

  FILE*  f = fopen("/spiffs/foo.txt", "r");
  if (f == NULL) {
      Serial.println("Failed to open file for reading");
      return;
  }
  
  char line[64];
  fgets(line, sizeof(line), f);
  fclose(f);
  Serial.println(line);
} 

void loop(){
}

void listDir(fs::FS &fs, const char * dirname, uint8_t levels) {
  Serial.printf("Listing directory: %s\r\n", dirname);

  File root = fs.open(dirname);
  if (!root) {
    Serial.println("- failed to open directory");
    return;
  }
  if (!root.isDirectory()) {
    Serial.println(" - not a directory");
    return;
  }

  File file = root.openNextFile();
  while (file) {
    if (file.isDirectory()) {
      Serial.print("  DIR : ");
      Serial.println(file.name());
      if (levels) {
        listDir(fs, file.name(), levels - 1);
      }
    } else {
      Serial.print("  FILE: ");
      Serial.print(file.name());
      Serial.print("\tSIZE: ");
      Serial.println(file.size());
    }
    file = root.openNextFile();
  }
}

How to read file on SPIFFS by fopen() ?

Thank you.

PeterR
Posts: 621
Joined: Mon Jun 04, 2018 2:47 pm

Re: How to access file on SPIFFS by fopen() on ESP32-Arduino?

Postby PeterR » Thu Oct 03, 2019 5:00 pm

SPIFFS allows read and write.
Your first example works because you create the file "hello" using the 'w' option. This will create the file if not present.

In your second example you attempt to open "foo.txt" as read only. This will not create a file if missing and so you get file error if missing.

You did not say how you initialised the SPIFFS so I am guessing that you have not initialised SPIFFS. If you intend to read from a file which you have not written to then you will need to upload a pre-written file system which contains "foo.txt"!

I am not familar with SPIFFS on Ardunio but there should be a spiffsgen.py or maybe mkspiffs.* lurcking somewhere.
& I also believe that IDF CAN should be fixed.

Who is online

Users browsing this forum: No registered users and 121 guests