Trouble getting not the empty line
Sep 28, 2014 at 4:43pm Sep 28, 2014 at 4:43pm UTC
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 ;
}
Sep 28, 2014 at 5:25pm Sep 28, 2014 at 5:25pm UTC
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;
}
Sep 28, 2014 at 5:34pm Sep 28, 2014 at 5:34pm UTC
it worked :)
Topic archived. No new replies allowed.