SPIFFS simple question - Writing data to file not working?

Hi everyone,

I have a ESP32 board and I'm trying to store data to a SPIFFS file.

This is how I'm doing:

  bool success = SPIFFS.begin();
   if(success){
    Serial.println("SPIFFS system mounted with success");
  }else{
    Serial.println("Error mounting the SPIFFS system");
    return;
  }

  file = SPIFFS.open("image.jpg", FILE_WRITE);

  Serial.println("Saving file...");
  for (int i = 0; i < rxValues.length(); i++){
    file.write(rxValues[i]);
    Serial.print(rxValues[i]);
  }
  Serial.println("");

  file.close();
  Serial.println("Closed file");

After execution I see this in the console:

16:58:52.312 -> Saving file...
16:58:52.312 -> ⸮⸮⸮⸮JFIF⸮⸮(ICC_PROFILE0mntrRGB XYZ acsp⸮⸮⸮-	desc⸮trXYZdgXYZxbXYZ⸮rTRC⸮(gTRC⸮(bTRC⸮(wtpt⸮cp
...
16:58:52.833 -> Closed media file

However, when I reboot my board even though it can find the file created, it says it has 0 bytes:

16:59:12.041 -> SPIFFS system mounted with success
16:59:12.076 -> Listing all files
16:59:12.144 -> FILE: /image.jpg **size: 0**

This is the code I'm using to list all files:

void listAllFiles(){

  Serial.println("Listing all files");
 
  File root = SPIFFS.open("/", FILE_READ);
  File file = root.openNextFile(FILE_READ);
 
  while(file){
    Serial.print("FILE: ");
    Serial.print(file.name());
    Serial.print(" size: ");
    Serial.println(file.size());
    file = root.openNextFile(FILE_READ);
  }
  root.close();
}

I don't know what is wrong here but because I'm assuming the file has been written correctly.
Any tips? Is there a way for me to peek into the content of my Flash RAM and open a file?

Thanks!

You're using SPIFFS and not LittleFS?

Maybe. I'm new to Arduino, appologies, from the research I did I thought it would be better to use SPIFFS in my ESP32. I also heard about FAT but ended up using SPIFFS.

I could switch to LittleFS - do you think this would be better and fix the issue?

Last time I tried SPIFFS, I got a compiler warning that SPIFFS is deprecated, and that I ought to use LittleFS.

For me, it was pretty much a simple text substitution

I replaced SPIFFS by LITTLEFS as you suggested.

However the same issue is there (image.jpg shows with size 0):

17:50:35.079 -> LITTLEFS system mounted with success
17:50:35.079 -> Listing all files
17:50:35.079 -> FILE: /image.jpg **size: 0**

In the console when I ran Serial.print(rxValues[i]); I see the values I want to be in the file.

I think the issue is that I was opening the file in FILE_WRITE mode instead of append and I guess the last chunk of bytes was empty. I changed to FILE_APPEND and it worked.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.