As we did in the previous posts, we need to include the SPIFFS.h library. That way, we will have access to the SPIFFS extern variable, which we will use to interact with the file system.
#include "SPIFFS.h"
Moving on to the Arduino setup, we start by opening a serial connection, so we can later output the results of our program. We will also mount the SPIFFS file system, using the begin method of the already mentioned SPIFFS extern variable.
Serial.begin(115200);
if(!SPIFFS.begin(true)){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
Now we will write some content to a file. Note that if you have already created and written to a file while following one of the previous tutorials, then you can skip this part if you want and use that file.
To open the file, we need to call the open method on the SPIFFS variable, passing as first input the name of the file to open and as second the opening mode. We will write to a file called “test.txt” and open the file in writing mode by passing as second parameter the constant FILE_WRITE.
This will return an object of class File and since this class overrides the C++ Boolean operator, we can check if the opening operation was successful simply by using an IF condition.
File file = SPIFFS.open("/test.txt", FILE_WRITE);
if(!file){
Serial.println("There was an error opening the file for writing");
return;
}
If the file was opened with success, we can proceed with writing some content to it. We will declare an arbitrary string and print its size before writing it to the file. Since I’m using a
const char * rather than the Arduino
String class, it is possible to obtain its size in bytes using the
strlen function.
const char * content = "Testing content!";
Serial.print("Content length: ");
Serial.println(strlen(content));
We then proceed with writing the content to the file and doing an error check to confirm that the content was written.
We finish the writing procedure by calling the close method on our file object.
if(file.print(content)){
Serial.println("File was written");;
} else {
Serial.println("File write failed");
}
file.close();
Now that we have the file with some content, we will open it in read mode, since we are no longer going to modify it but rather simply read its size.
We do that again by using the open method of the SPIFFS variable but, this time, we don’t need to explicitly pass the opening mode since that is an optional argument that defaults to reading mode (the one we want). This will again return an object of class File.
After that, we check again if the file was open successfully before trying to use it.
File file2 = SPIFFS.open("/test.txt");
if(!file2){
Serial.println("Failed to open file for reading");
return;
}
On success, we can obtain the size of the file simply by calling the size method on our File object. This method takes no arguments and returns the size of the file, in bytes.
Serial.print("File size: ");
Serial.println(file2.size());
To finalize the procedure, we close the file.
file2.close();
The final source code can be seen below.
#include "SPIFFS.h"
void setup() {
Serial.begin(115200);
if(!SPIFFS.begin(true)){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
File file = SPIFFS.open("/test.txt", FILE_WRITE);
if(!file){
Serial.println("There was an error opening the file for writing");
return;
}
const char * content = "Testing content!";
Serial.print("Content length: ");
Serial.println(strlen(content));
if(file.print(content)){
Serial.println("File was written");;
} else {
Serial.println("File write failed");
}
file.close();
File file2 = SPIFFS.open("/test.txt");
if(!file2){
Serial.println("Failed to open file for reading");
return;
}
Serial.print("File size: ");
Serial.println(file2.size());
file2.close();
}
void loop() {}
没有评论:
发表评论