0

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.

asked Nov 12, 2021 at 16:58
1
  • Just open the file. There is no "not completely removed". Commented Nov 12, 2021 at 17:07

1 Answer 1

3

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.

answered Nov 12, 2021 at 17:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.