Hi,
Once SD.open(filename, FILE_WRITE) it erases whole file content. According to SD.h spec:
"FILE_WRITE: open the file for reading and writing, starting at the end of the file."
In my understanding, it should append new content to an existing one.
Has anyone faced this problem before? Is there someone who can help me?
Code:
#include <SD.h>
String filename = "/new_file.txt";
File file;
void setup() {
pinMode(5, OUTPUT);
SD.begin(5);
Serial.begin(115200);
Serial.println("Setup Start");
file = SD.open(filename, FILE_WRITE);
file.println("First line");
file.close();
Serial.println("first print:");
PrintFile();
file = SD.open(filename, FILE_WRITE);
file.println("Second Line");
file.close();
Serial.println("second print:");
PrintFile();
}
void loop() {
// put your main code here, to run repeatedly:
}
void PrintFile() {
file = SD.open(filename, FILE_READ);
String file_data = "";
while(file.available()){
file_data += (char)file.read();
}
Serial.println(file_data);
file.close();
}
Serial Monitor output:
Setup Start
first print:
First line
second print:
Second Line