ESP8266 SPIFFS file reading issue

I'm testing the SPIFFS capability on the ESP8266-01 module.
I have a simple text file "/ssid" with a 29 char string in it. Uploaded fine.
I read it using FS.h header include, I create a 30 char array and pass the file path and a pointer to the char arr, it opens the file and reads ok but returns a 36 char string, with some unwanted char which appear as ? char in the serial monitor.

  //read the ssid of the switch in AP mode
  const char* SSID_FILE = "/ssid";
  char ssid[29];
  readFile( SSID_FILE, ssid );
  Serial.println("reading ssid file into a char str"); // this works
  size_t len = strlen( ssid );
  Serial.print("the ssid var size is ");
  Serial.println( len );
  Serial.print("the ssid returned is ");
  Serial.println(ssid); 

The output is

21:51:15.869 -> Reading file: /ssid
21:51:15.869 -> fs mounted ok
21:51:15.869 -> file opened ok
21:51:15.869 -> reading file contents
21:51:15.903 -> the file size is 30
21:51:15.903 -> closing file and fs
21:51:15.903 -> reading ssid file into a char str
21:51:15.903 -> the ssid var size is 36
21:51:15.903 -> the ssid is Datakey-SSID-wifiSwitch-55YYw⸮⸮⸮⸮⸮⸮⸮

The function is simple.

bool readFile( const char * path, char* outStr ) {

  Serial.printf("Reading file: %s\r\n", path);

  if ( !SPIFFS.begin() ) {
    Serial.println("Error mounting the file system");
    return false;
  } else {
    Serial.println("fs mounted ok");
  }
  //open the file in read mode
  File file = SPIFFS.open( path, "r");
  if ( !file ) {
    Serial.println("failed to open file");
    return false;
  } else {
    Serial.println("file opened ok");  
  }
  Serial.println("reading file contents");
  Serial.print("the file size is ");
  Serial.println(file.size()); 
  
  while ( file.available()  )  {
    outStr[i] = file.read();
    i++;
  }
  
  Serial.println("closing file and fs");
  file.close();
  SPIFFS.end();
  return true;
}

If I increase the ssid arr from 29 to say 50 I get a str of 68 char, with lots of backward question marks.
The file being read is clean and has no special char, and no \n\r etc
The hardware may be rubbish, as it was v cheap.
Any thoughts?

Welcome to the forum

How was the 29 char string put in the file ?

1 Like

Don't work too hard on the SPIFFS filesystem.

SPIFFS Deprecation Warning

SPIFFS is currently deprecated and may be removed in future releases of the core. Please consider moving your code to LittleFS. SPIFFS is not actively supported anymore by the upstream developer, while LittleFS is under active development, supports real directories, and is many times faster for most operations.
https://arduino-esp8266.readthedocs.io/en/latest/filesystem.html

Thank you for the warning! I didn't know this. I'll still keep trying a little longer, I like to solve puzzles!

It was typed in a text editor and uploaded with the SPIFFS plugin.
I also tried writing to the file in a terminal.

rupert@asus-laptop: echo "Datakey-SSID-wifiSwitch-55YYw" > ssid

Oddly the wc program reports 30 bytes and 30 characters,

wc -m ssid  //30
wc -c  ssid  //30

The file navigator states 30 bytes, but there are def 29 visible char. There is no \r\n char visible or written, is there something hidden in the file creation that I can't see? And is it corrupting my reading of the file?

In C/C++, character strings are terminated by a required zero byte, which is not counted.

This can hold only 28 characters:
char ssid[29];

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