Hello everyone, I guess I missing something, but I have a problem with file storage. After the reboot, I can not able to read the file which I created before the reboot. My code:
// ===============
// Step 1 - loading saved data if it exists
// ===============
struct stat st;
stat("/plate.dat", &st);
long size = st.st_size;
if (size!=310200) // Check that file has a correct size
{
Serial.printlnf("%s: Error: can not load save data, file size = %d", __func__, size);
return;
}
// .....
int rFd = open("/plate.dat", O_RDONLY);
if (rFd==-1)
{
Serial.printlnf("%s: Error Open file! ", __func__);
return;
}
for (int i=0; i<MAX_X*MAX_Y/8/100; i++ )
{
read(rFd, buff, SRAM_BUFF_LEN);
// ...
}
close(rFd);
// ===============
// Step 2 - save data to file
// ===============
int rFd = open("/plate.dat", O_RDWR | O_CREAT | O_TRUNC);
if (rFd == -1)
{
Serial.printlnf("%s: Error Can not open file!", __func__);
}
for (int i=0; i<MAX_X*MAX_Y/8/100; i++ )
{
// ...
write(rFd, buff, SRAM_BUFF_LEN);
}
fsync(rFd);
close(rFd);
// And theck the file size right after it close:
struct stat st;
stat("/plate.dat", &st);
long size = st.st_size;
Serial.printlnf("%s: Save to file %d bytes", __func__, size);
The first run:
Step 1 - file not exist,
Step 2 - file wrote without any errors. The file size is 310200 bytes.
After reboot:
Step 1 - file exists, but the file size is 19 bytes (instead of 310200 bytes).
Step 2 - file wrote without any errors. The file size is 310200 bytes.
So I can not realize what could happen with the file after reboot, that its size is changed. Just in case, am I right that the file system storing in nonvolatile memory?
My device:
Device OS: 2.0.0
Type: Asset Tracker