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
}
asked Sep 4, 2018 at 20:11

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);
 }
}
answered Sep 5, 2018 at 14:34
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());
}
answered Sep 4, 2018 at 20:13
6
  • @michael how do i block/skip the first line and read the 2nd line up to the last line? 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 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? Commented Sep 8, 2018 at 14:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.