Delete contents of a text or binary file on SD card

If you want to delete the content of an existing file just open it with the additional mode 'O_TRUNC'.
The code might look like this:

myFile = SD.open("filename", FILE_WRITE | O_TRUNC);

A complete list of all modes is here:

  • FILE_READ: open the file for reading, starting at the beginning of the file.
  • FILE_WRITE: open the file for reading and writing, starting at the end of the file.
  • O_READ: open the file for reading, starting at the beginning of the file.
  • O_RDONLY: same as O_READ.
  • O_WRITE: open() file for writing, starting at the beginning of the file.
  • O_WRONLY: same as O_WRITE
  • O_RDWR: is a compination of (O_READ | O_WRITE)
  • O_ACCMODE: is a combination of (O_READ | O_WRITE)
  • O_APPEND: makes only sense with O_WRITE. Writing will be continued at the and end of the file
  • O_SYNC: call sync() after each write
  • O_CREAT: create the file if nonexistent */
  • O_EXCL: If O_CREAT and O_EXCL are set, open() shall fail if the file exists
  • O_TRUNC: truncate the file to zero length

Might be helpful
Heinz :slightly_smiling_face:

2 Likes

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.