1

is there a way to read only 2nd up to the last line except for the first line?

here's my sample code

File myFile;
char cr;
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


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

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");



  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());



    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  // nothing happens after setup
}

2 Answers 2

3

skip first line

bool firstLine = true;
while (myFile.available()) {
  int c = myFile.read();
  if (firstLine) {
    if (c == '\n') {
      firstLine = false;
    }
  } else {
    Serial.write(c);
  }
}
0
3

Why don't you do reads for the first line, ignore them and start the while thus:

while (myFile.available() && (myFile.read() != '\n') {}; // Empty while body
while (myFile.available()) {
  Serial.write(myFile.read());
}
6
  • @michael how do i block/skip the first line and read the 2nd line up to the last line?
    – awesome23
    Commented Sep 4, 2018 at 20:40
  • By reading the first line and don't use it as in the code above. Commented Sep 4, 2018 at 20:57
  • myFile.read(); reads only one byte/char
    – Juraj
    Commented Sep 5, 2018 at 14:30
  • @Juraj Thanks and improved my answer accordingly Commented Sep 5, 2018 at 16:15
  • and what for is the endOfLineFound test in second while?
    – Juraj
    Commented Sep 8, 2018 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.