I have a datafile, datalog.txt I can open, read and write cool. What is the best way to delete all the file contents? Should I try rewriting the SD card file with NULL data? I have been reading the "SD.h" header file. I see a flush() and truncate() function should I use one of these?
or maybe I should just close and reopen the file, and overwrite the data. But I'm concerned that this might lead to some miss representations if the old data is not completely removed.
I am using a teensy 4.1 with built_in SD card.
-
Just open the file. There is no "not completely removed".Majenko– Majenko11/12/2021 17:07:09Commented Nov 12, 2021 at 17:07
1 Answer 1
To remove a file use SD.remove("datalog.txt");
SD library's FILE_WRITE
is O_READ | O_WRITE | O_CREAT | O_APPEND
so it would append at the end of the existing file.
(削除) to start with an empty file: File file = FS.open("datalog.txt", O_READ | O_WRITE | O_CREAT); (削除ここまで)
EDIT: opening the file with O_READ | O_WRITE | O_CREAT doesn't clear the content of the file, only starts to overwrite it from the beginning.