I created a quick example for you.
I have a text file saved on the SD card.
Text line 1
Text line 2
TEXT LINE 3
Text line 4
Text line B
I want to open that file and rewrite the content making adjustments. e.g. correcting to lower case in line 3 and removing line 5
So her is the code I came up with
File myFile = SD.open("MY_TEXT.TXT", FILE_READ);
if (myFile) {
File tempFile = SD.open("TMP_TEXT.TXT", FILE_WRITE);
if (tempFile) {
while (myFile.available()) {
String line = myFile.readStringUntil('\n');
if (line.substring(0, line.length() - 1); == "TEXT LINE 3") { //substring removes the end of line for comparison
tempFile.println("Text line 3");
} else if (line != "Text line B") {
tempFile.println(line);
}
}
myFile.close(); tempFile.close();
} else Serial.println("Error creating temporary file.");
} else Serial.println("Error opening MY_TEXT.TXT");
This code does not work and all entries are rewriten to a new file. Why. Any ideas?
The colon was only a typo. But I found the silly problem with my code. The second condition should also have substring in it. I am closing this message.
Thanks for all the feedbacks:)