Skip to content

Instantly share code, notes, and snippets.

@CelliesProjects
Last active September 1, 2023 18:04
Show Gist options
  • Save CelliesProjects/7fab9013517583b3a0922c0f153606a1 to your computer and use it in GitHub Desktop.
Save CelliesProjects/7fab9013517583b3a0922c0f153606a1 to your computer and use it in GitHub Desktop.
Read and write a c++ struct to a file. Arduino IDE. ESP32.
/*
* EXAMPLE READING AND WRITING A C++ STRUCT TO A FILE
*/
#include <FS.h>
#include <FFat.h>
const char *fileName = "/somefile.txt";
struct aStruct {
char someString[11];
float someFloat;
};
void setup() {
if (!FFat.begin()) {
log_e("FFat Mount Failed");
while (1)
delay(100);
}
aStruct firstStruct = { "1234567890", 1.7 }; // String is 10 chars plus a terminator '\0'
log_i("struct size: %i", sizeof(aStruct));
File file = FFat.open(fileName, FILE_WRITE);
const size_t bytesWritten = file.write((byte *)&firstStruct, sizeof(firstStruct));
file.close();
if (bytesWritten < sizeof(aStruct)) {
log_e("Write error");
}
}
void loop() {
aStruct secondStruct = { "----------", 0.0 };
File file = FFat.open(fileName, FILE_READ);
const size_t bytesRead = file.read((byte *)&secondStruct, sizeof(secondStruct));
log_i("file size: %i", file.size());
if (bytesRead < file.size()) {
log_e("Read error");
}
file.close();
log_i("Data: %s, %.2f\n", secondStruct.someString, secondStruct.someFloat);
while (1)
delay(100);
}
@Guihgo
Copy link

Guihgo commented Dec 14, 2020

thanks

@Tszul
Copy link

Tszul commented Mar 27, 2021

THANKS

@mosigi-kim
Copy link

thanks

@ahreenah
Copy link

Thanks a lot!

@heliophagus
Copy link

The code doesn't work as posted, at least for me. No errors, but the read operation does not work. I tested this by changing the values in the struct after writing the struct to the file; after reading from the file, the original values written to the file are not present. Any ideas on why?

`
/*
EXAMPLE READING AND WRITING A C++ STRUCT TO A FILE
*/
#include <FS.h>
#include <SPIFFS.h>

typedef struct {
float EEE = 0.0;
int8_t mark = -1;
} SPIFFdata;

SPIFFdata sd;

void setup() {
// put your setup code here, to run once:
Serial.begin(115200);

if (!SPIFFS.begin()) {
Serial.println("Card Mount Failed");
return;
}

sd.EEE = 123.456;
sd.mark = 65;

// strncpy( myStruct.someString, "testtesttest", sizeof(myStruct.someString) );
File myFile = SPIFFS.open("/SPIFFtest1.bin", FILE_WRITE);
myFile.write((byte *)&sd, sizeof(sd));
myFile.close();
}

void loop() {
// put your main code here, to run repeatedly:
// File myFile = SPIFFS.open("/SPIFFtest1.bin", FILE_WRITE);

// overwrite the values in the struct that were written to the file in setup()
sd.EEE = 0.0;
sd.mark = -1;

File myFile = SPIFFS.open("/SPIFFtest1.txt", FILE_WRITE);
myFile.read((byte *)&sd, sizeof(sd));
myFile.close();
Serial.println("File contains:");
Serial.println(sd.EEE, 3);
Serial.println(sd.mark);

//Serial.printf( "read: %s, %.2f\n", myStruct.someString, myStruct.someFloat );
delay(1000);
}
`

In the serial monitor window, I just get:

File contains:
0.000
-1

@CelliesProjects
Copy link
Author

CelliesProjects commented Aug 22, 2022

No, not really. You are using Arduino ESP32 Core 2.0.3 or later? Try a lower version number like 1.0.6. It might solve this issue.

@edgar-bonet
Copy link

@heliophagus: Your code does not check the return value of myFile.read(). The call probably failed, given that myFile has been opened in FILE_WRITE mode.

@CelliesProjects
Copy link
Author

CelliesProjects commented Mar 21, 2023

Cleaned up the code. It is now checking the amount that has been written or read.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment