3

Running Arduine IDE SD DataLogger Example, my data gets appended to a txt file.

File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
 dataFile.println(dataString);
 dataFile.close();
}

Is there an explicit option to open the file in overwrite / append modes?

Michel Keijzers
13k7 gold badges41 silver badges58 bronze badges
asked May 30, 2019 at 23:41
0

4 Answers 4

3

If you look in this library, you see:

File SDClass::open(const char *filepath, uint8_t mode) {
 ...
 if ((mode & (O_APPEND | O_WRITE)) == (O_APPEND | O_WRITE)) {

So you can use all these mode combinations (e.g. O_CREATE, O_APPEND, O_WRITE).

answered May 31, 2019 at 1:01
1
  • 1
    answered the question as asked. UPVOTED & ACCEPTED :) Commented Feb 24, 2020 at 21:39
4

The Arduino SD library is an Arduino wrapper of old version of SdFat library (put into utility subfolder of the SD library). This SdFat library has constants like O_READ, O_WRITE, O_APPEND.

Arduino wrapper has constants

#define FILE_READ O_READ
#define FILE_WRITE (O_READ | O_WRITE | O_CREAT | O_APPEND)

You can use the SdFa library constants in the wrapper calls.

File dataFile = SD.open("datalog.txt", O_READ | O_WRITE | O_CREAT);

Warning: not all versions of SD library bundled in different board packages have O_APPEND in #define FILE_WRITE. Even in the Arduino SD library the O_APPEND was removed some time ago and then the change was reverted, because all dataloger examples used FILE_WRITE.

answered May 31, 2019 at 5:02
0
4

You only need to open the file with FILE_WRITE and use file.seek(EOF) to go to de end of the file. After that you can write whatever you want that will be appended to the end of the file.

File outputFile = SD.open(LOG_FILE, FILE_WRITE);
outputFile.seek(EOF);
outputFile.println("Appended to the EOF");
answered Feb 17, 2020 at 18:28
1
  • perfect for day-to-day solutions, thanks! UPVOTED Commented Feb 24, 2020 at 21:37
1
File outputFile = SD.open(LOG_FILE, MODE);

With Arduino PCB: MODE=FILE_WRITE to append the data.

With ESP32 pcb, V 3.1.1: FILE_WRITE to overwrite the data. FILE_APPEND to append data.

Append:

File outputFile = SD.open("/LOG_FILE", FILE_APPEND);

Note:

"/LOG_FILE" for Linux, "\LOG_FILE" for Windows.

answered Jan 29 at 21:27
1
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. Commented Jan 29 at 21:34

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.