I'm trying to read txt file (has numeric values) line by line. I used SPIFFS and I used this function
void readFile(fs::FS &fs, const char * path){
Serial.printf("Reading file: %s\r\n", path);
File file = fs.open(path);
if(!file || file.isDirectory()){
Serial.println("− failed to open file for reading");
return;
}
int count = 0;
Serial.println(" read from file:");
while(file.available()){
if (count < 100)
Serial.write(file.read());
}
}
What is the alternative function for "file.read()" something like "readline" because I need to read the file from first line to 100 and from 101 to 200 and so on .
CodePudding user response:
To read lines from Serial, network Client, from File or other object implementing the Arduino Stream class you can use function readBytesUntil.
uint8_t lineBuffer[64];
while (file.available()) {
int length = file.readBytesUntil('\n', lineBuffer, sizeof(lineBuffer) - 1);
if (length > 0 && lineBuffer[length - 1] == '\r') {
length--; // to remove \r if it is there
}
lineBuffer[length] = 0; // terminate the string
Serial.println(lineBuffer);
}
CodePudding user response:
The File is a Stream and has all of the methods that a Stream does, including readBytesUntil and readStringUntil.