Trouble getting not the empty line

This is a function in my program, it needs to get the last line of a file, but some files that i need to run it has empty line as last line so in that cases i need the line witch is not empty, can somebody help me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void findlastline(ifstream& file, string& lastline)
{
    while(file >> lastline)
    {
        getline(file, lastline);
        if (lastline.length() == 0)
        {
            while(!file.eof())
            getline(file, lastline);
        }
        else
        getline(file, lastline);
    }
    
    return;
}
1
2
3
4
5
6
7
8
9
10
11
12
void findlastline(ifstream& file, string& lastline)
{
    std::string prev, current;

    while (getline(file, current))
    {
        if (!current.empty())
            prev = current;
    }

    lastline = current.empty() ? prev : current;
}

it worked :)
Topic archived. No new replies allowed.