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
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
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
-
@michael how do i block/skip the first line and read the 2nd line up to the last line?awesome23– awesome232018年09月04日 20:40:12 +00:00Commented Sep 4, 2018 at 20:40
-
By reading the first line and don't use it as in the code above.Michel Keijzers– Michel Keijzers2018年09月04日 20:57:07 +00:00Commented Sep 4, 2018 at 20:57
-
myFile.read(); reads only one byte/char2018年09月05日 14:30:08 +00:00Commented Sep 5, 2018 at 14:30
-
@Juraj Thanks and improved my answer accordinglyMichel Keijzers– Michel Keijzers2018年09月05日 16:15:46 +00:00Commented Sep 5, 2018 at 16:15
-
and what for is the endOfLineFound test in second while?2018年09月08日 14:39:04 +00:00Commented Sep 8, 2018 at 14:39
lang-cpp