Reputation: 1193
Happy New Year, everyone!
I have a text file that looks like this:
A|AAAAA|1|2
R|RAAAA
R|RAAAA
A|BBBBB|1|2
R|RBBBB
R|RBBBB
A|CCCCC|1|2
R|RCCCC
The following code searches for the relevant text in the file based on the key and returns all the lines that belong to the key:
while( std::getline( ifs, line ) && line.find(search_string) != 0 );
if( line.find(search_string) != 0 )
{
navData = "N/A" ;
}
else{
navData = line + '\n' ; // result initially contains the first line
// now keep reading line by line till we get an empty line or eof
while( std::getline( ifs, line ) && !line.empty() )
{
navData += line + '\n';
}
}
ifs.close();
return navData;
In Windows I get what I need:
A|BBBBB|1|2
R|RBBBB
R|RBBBB
In Mac, however, code "&& !line.empty()" seems to get ignored, since I get the following:
A|BBBBB|1|2
R|RBBBB
R|RBBBB
A|CCCCC|1|2
R|RCCCC
Does anyone know why?
Cheers, everyone!
Upvotes: 1
Views: 240
Reputation: 31637
Windows and Mac have different opinions about how an empty line looks like. On Windows, lines are teminated by "\r\n"
. On Mac, lines are terminated by "\n"
and the preceding "\r"
leads to the line not being empty.
Upvotes: 4