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
}
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);
  }
}
| improve this answer | |
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());
}
| improve this answer | |
  • @michael how do i block/skip the first line and read the 2nd line up to the last line? – awesome23 Sep 4 '18 at 20:40
  • By reading the first line and don't use it as in the code above. – Michel Keijzers Sep 4 '18 at 20:57
  • myFile.read(); reads only one byte/char – Juraj Sep 5 '18 at 14:30
  • @Juraj Thanks and improved my answer accordingly – Michel Keijzers Sep 5 '18 at 16:15
  • and what for is the endOfLineFound test in second while? – Juraj Sep 8 '18 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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