String comparison dilemma when reading content from SD card

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?

This not is a code. Its is a snippet of code.
Please, post complete code.

How many files can you have open at the same time when using the SD library ?

Not sure how many but with this version of the library sketch opens both files so I presume 2 is possible

The second file is created and I can see in it
Text line 1

Text line 2

TEXT LINE 3

Text line 4

Text line B
I have extra lines as I write using println. And the line read from original code containg end of line character. Thus extra lines

#include <SPI.h>
#include <SD.h>

const int chipSelect = 10;


void setup() {
  Serial.begin(9600);

  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    while (1);
  } else{
  Serial.println("Initialization done.");
  }
   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");

}

void loop() {
  // put your main code here, to run repeatedly:

}

What is the purpose of the semi-colon in the above line of code?

Do you get any error or warning produced?

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:)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.