1 week, 3 days ago.

LittleFileSystem fopen("file","w") does remove existing file

According to the C++ fopen documentation at if 'mode' is "w" and the filename points to an existing file, the contents of the existing file are cleared.

FATFileSystem does have this behaviour but LittleFileSystem does not seem to. Here's some example code....


  char * filename = "/fs/eventlog.bin";
  FILE *fd = fopen(filename,"w");
  struct stat stat_buf;
  stat(filename, &stat_buf);
  ASSERT(stat_buf.st_size!=0,"file size should be zero after opening");

When the underlying filesystem is LittleFileSystem and if the file already exists and it not empty, this asserts since the filesize is not changed on fopen.

Obviously a workaround is to use the remove method (or possibly a zero-length write?)

Thanks, reported here.

posted by Jan Jongboom 10 Jan 2018
Comment on this question

1 Answer

1 week, 1 day ago.

Thanks for reporting, looks like this was a valid bug related to how LittleFS was handling the O_TRUNC flag. Here's the patch to fix it:

You should be able to get the patch in the next patch release of Mbed OS or so (5.7.3?)

To post an answer, please log in.