Which SD library? If you compile with Verbose output (and everyone should), at the end you should see something like
Using library SD at version 1.3.0 in folder: /Users/kenb4/Library/Arduino15/libraries/SD
On this Mac, it's the generic SD library baked in at the IDE/CLI level, and neither board- nor library-specific. Anyway, if you Go to Definition on SD.open, it takes you to libraries/SD/src/SD.h
File open(const char *filename, uint8_t mode = FILE_READ);
The default mode is FILE_READ. Drilling in further
#define FILE_READ O_READ
#define FILE_WRITE (O_READ | O_WRITE | O_CREAT | O_APPEND)
FILE_WRITE includes O_APPEND, which is listed among the others in libraries/SD/src/utility/SdFat.h
// use the gnu style oflag in open()
/** open() oflag for reading */
uint8_t const O_READ = 0X01;
/** open() oflag - same as O_READ */
uint8_t const O_RDONLY = O_READ;
/** open() oflag for write */
uint8_t const O_WRITE = 0X02;
/** open() oflag - same as O_WRITE */
uint8_t const O_WRONLY = O_WRITE;
/** open() oflag for reading and writing */
uint8_t const O_RDWR = (O_READ | O_WRITE);
/** open() oflag mask for access modes */
uint8_t const O_ACCMODE = (O_READ | O_WRITE);
/** The file offset shall be set to the end of the file prior to each write. */
uint8_t const O_APPEND = 0X04;
/** synchronous writes - call sync() after each write */
uint8_t const O_SYNC = 0X08;
/** create the file if nonexistent */
uint8_t const O_CREAT = 0X10;
/** If O_CREAT and O_EXCL are set, open() shall fail if the file exists */
uint8_t const O_EXCL = 0X20;
/** truncate the file to zero length */
uint8_t const O_TRUNC = 0X40;
The file offset shall be set to the end of the file prior to each write. I don't have an SD handy, so I tried this in the simulator
File textFile = SD.open("wokwi.txt", FILE_WRITE);
Serial.println(textFile.position());
char buf[20];
textFile.seek(0);
buf[textFile.read(buf, sizeof(buf) - 1)] = 0;
Serial.println(buf);
textFile.write("first line\n");
Serial.println(textFile.position());
buf[textFile.read(buf, sizeof(buf) - 1)] = 0;
Serial.println(buf);
textFile.seek(0);
buf[textFile.read(buf, sizeof(buf) - 1)] = 0;
Serial.println(buf);
textFile.write("second line\n");
textFile.close();
// Example of reading file from the card:
textFile = SD.open("wokwi.txt");
The file starts with a line of text. With or without the seek, the new lines were added to the end of the file.
- when opened to
O_APPEND, the initial position is at the end of the file
- therefore to
read anything, first seek the beginning
- writing appends automatically, updating the
position to the new end of file
- the second
read gets nothing
- the third
read gets the first line again, plus some of the newly added line
So if you are only appending when you are writing, O_APPEND should work. If you are writing elsewhere, then you have to come up with your own combination of modes, and you'll need to keep an eye of where you seek, and make sure it is not some crazy location like 1070523488. If that happens, then the problem is where that number came from.