ESP32 Arduino SPIFFS: Append content to file

In this tutorial we will check how to append content to a file on the ESP32 SPIFFS file system. The tests were performed using a DFRobot’s ESP32 module integrated in a ESP32 development board.


Introduction

In this tutorial we will check how to append content to a file on the ESP32 SPIFFS file system.

We will start by opening a file for writing and write a line of text to it. For a detailed guide on how to open a file for writing, please check here. Then, after closing the file, we will open it again but this time we will append the content, ensuring that we do not overwrite the original text.

Finally, we will read the content of the whole file to confirm the full text is there. For a tutorial on how to read from a file in the ESP32 SPIFFS file system, please check here.

The tests were performed using a DFRobot’s ESP32 module integrated in a ESP32 development board.

 

The code

As we have been doing in the previous posts, we start with the SPIFFS.h library include, in order to access the SPIFFS extern variable, needed to interact with the file system. This is the only library we will need.

#include "SPIFFS.h"

Then we move on to the Arduino setup, where we will write the rest of the code. We start by opening a serial connection to output the results of the program execution.

Serial.begin(115200);

Then we mound the SPIFFS file system simply by calling the begin method on the SPIFFS external variable. Remember that it becomes available by including the SPIFFS.h library. The “true” Boolean value passed as input of the method will ensure the file system is formatted in case mounting fails.

if(!SPIFFS.begin(true)){
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
}

After the file system is mounted, our first block of code will take care of writing some content to the file, in writing mode. This will overwrite the file if it already exists.

Like covered in detail in this previous tutorial, in order to write to a file we first need to open it using the open method of the SPIFFS extern variable, passing as first input the name of the file and as second the opening mode.

We will write in a file called “/test.txt” and since we want to open it in write mode, we pass the constant FILE_WRITE as second argument of the open method.

This call will return an object of class File, which we will enclose in an IF clause to check if some error occurred during the opening procedure. This is possible since the File class overrides the C++ Boolean operator.

If the procedure was successful, then we can write to the file simply using the println method on the File object, passing as input the content to write. The println function will add a new line character a the end of the content.

Finally, we close the file with a call to the close method on the File object.

File fileToWrite = SPIFFS.open("/test.txt", FILE_WRITE);

if(!fileToWrite){
    Serial.println("There was an error opening the file for writing");
    return;
}

if(fileToWrite.println("ORIGINAL LINE")){
    Serial.println("File was written");;
} else {
    Serial.println("File write failed");
}

fileToWrite.close();

Now that we have finished the initial writing procedure, we will add an extra line of content to our file. To do it, we will follow the same exact procedure, with the exception in the opening mode.

So, this time, we will open the file in append mode, which will ensure that the written content will be added at the end of the file, without overwriting the previously existing content.

To set the opening method as append, we pass the constant FILE_APPEND to the open method of the SPIFFS variable.

File fileToAppend = SPIFFS.open("/test.txt", FILE_APPEND);

After that we do exactly the same check with an IF condition, to make sure the file was correctly opened, and then we write the new line with the println method call on the File object.

if(!fileToAppend){
    Serial.println("There was an error opening the file for appending");
    return;
}

if(fileToAppend.println("APPENDED LINE")){
    Serial.println("File content was appended");
} else {
    Serial.println("File append failed");
}

After appending the content, we close the file again.

fileToAppend.close();

To finalize, we will read the content of the file to make sure both the original line and the appended one are there.

To do this, we will again open the file, but this time in reading mode, and then print all its content while there are bytes to read. For a detailed explanation on the procedure to read a file from the ESP32 SPIFFS file system, please check this previous post.

File fileToRead = SPIFFS.open("/test.txt");

if(!fileToRead){
    Serial.println("Failed to open file for reading");
    return;
}

Serial.println("File Content:");

while(fileToRead.available()){

   Serial.write(fileToRead.read());
}

fileToRead.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;
   }

    //--------- Write to file
    File fileToWrite = SPIFFS.open("/test.txt", FILE_WRITE);

    if(!fileToWrite){
        Serial.println("There was an error opening the file for writing");
        return;
    }

    if(fileToWrite.println("ORIGINAL LINE")){
        Serial.println("File was written");;
    } else {
        Serial.println("File write failed");
    }

    fileToWrite.close();

    //--------- Apend content to file
    File fileToAppend = SPIFFS.open("/test.txt", FILE_APPEND);

    if(!fileToAppend){
        Serial.println("There was an error opening the file for appending");
        return;
    }

    if(fileToAppend.println("APPENDED LINE")){
        Serial.println("File content was appended");
    } else {
        Serial.println("File append failed");
    }

    fileToAppend.close();

    //---------- Read file
    File fileToRead = SPIFFS.open("/test.txt");

    if(!fileToRead){
        Serial.println("Failed to open file for reading");
        return;
    }

    Serial.println("File Content:");

    while(fileToRead.available()){

        Serial.write(fileToRead.read());
    }

    fileToRead.close();
}

void loop() {}

 

Testing the code

To test the code, simply use the Arduino IDE to compile it and upload it to your ESP32 device. When the procedure finishes, open the Arduino IDE serial monitor.

As shown in figure 1, you should get the full content of the file, with both the originally written line and the appended one, as expected.

ESP32 SPIFFS file system append to file.png

Figure 1 – Appending content to a file in the ESP32 SPIFFS file system.

 

Related posts

Advertisements

5 Replies to “ESP32 Arduino SPIFFS: Append content to file”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s