How to know if it is end of file

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?

data.available() does work. Your code isn't testing for end of file properly. When you read the last line, pos will be set to the position of the last '\n' in the file and then you return from the function. The next time you call ReadFromFile() it will seek to 'pos' and then try to read beyond the end of file. You need to test data.available() at the beginning of the loop. You also need to figure out how to indicate to the calling program that end of file has occurred. ReadFromFile does not return a value so there's no way for your calling code to know that the end of file has been reached.

Opening and closing the file for each line is very inefficient. It would be better to open the file once, outside the function and close it when end of file is reached.

Pete

el_supremo: data.available() does work. Your code isn't testing for end of file properly. When you read the last line, pos will be set to the position of the last '\n' in the file and then you return from the function. The next time you call ReadFromFile() it will seek to 'pos' and then try to read beyond the end of file. You need to test data.available() at the beginning of the loop. You also need to figure out how to indicate to the calling program that end of file has occurred. ReadFromFile does not return a value so there's no way for your calling code to know that the end of file has been reached.

Opening and closing the file for each line is very inefficient. It would be better to open the file once, outside the function and close it when end of file is reached.

Pete

I appreciate your suggestions. I'll make the changes and then return to give feedback.