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?