Write data from an array to SPIFFS

Hi!

I need to save an array of Strings in a SPIFFS file on my ESP32, and then be able to read that file and transfer the value to a new array string variable, but I can't do it and I can't figure out where my error is.

My code:

#include <SPIFFS.h>

String a[] = {"One", "Two", "Three", "Four"};
String b[] = {"eE", "fF", "gG"};
String c[99];


void setup() {
  Serial.begin(115200);
  SPIFFS.begin();
  writeSpiffs(a, 4);
  readSpiffs("/testOff.txt", 4);
  Serial.println(c[0]);
}

void loop() {


}

void writeSpiffs(String a[], int cont) {
  File f = SPIFFS.open("/testOff.txt", "w+");
  for (int i = 0; i < cont; i++) {
    f.print(a[i]);
  }
  f.close();
}

void readSpiffs(String spi, int cont) {
  File file = SPIFFS.open(spi);
  if (!file) {
    Serial.println("Error " + spi);
  }
  else {
    while (file.available()) {
      for (int i = 0; i < cont; i++) {
        c[i] = file.readString();
        Serial.println(c[i]);
      }
    }
  }
  file.close();
}

The result on the serial monitor:

OneTwoThreeFour

OneTwoThreeFour

Could anyone tell me how I could do this?

Thanks!

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