Hey! I'm trying to read some data from a SD Card but I can't detect the end of the file. Searching a little, I found that
file.available()
should return false when I get in the end of the file, but it didn't work. Here follows my code
void readFromFile()
{
int i = 0;
char inputs[27];
char inputc;
data = SD.open("log.txt");
if(data) {
data.seek(pos);
do{
inputc = data.read();
switch (inputc) {
case '\n':
pos = data.position();
inputs[i] = '\0';
// Serial.println(inputs);
i = 0;
break;
case ' ':
// do nothing
break;
default:
inputs[i] = inputc;
i++;
}
} while (data.available() && inputc != '\n');
}else {
Serial.println("error opening file");
}
Serial.println(inputs);
data.close();
}
I need to process line by line, thats why I use
while (data.available() && inputc != '\n')
I also use seek() and position() functions to return reading from the place it were before.
When I get the last line, it shows me some memory trash. How can I detect the end of the file?