getline && line.empty problem

Happy New Year, everyone!

I have a text file that looks like this:

1
2
3
4
5
6
7
8
9
10
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
   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:
1
2
3
A|BBBBB|1|2
R|RBBBB
R|RBBBB


In Mac, however, code "&& !line.empty()" seems to get ignored, since I get the following:
1
2
3
4
5
6
A|BBBBB|1|2
R|RBBBB
R|RBBBB

A|CCCCC|1|2
R|RCCCC


Does anyone know why?

Cheers, everyone!
Get a debugger and watch the "empty" string in question, you may note that it actually has one character.
I guess that your issue is related to the different line break representation between OS http://en.wikipedia.org/wiki/Newline
Thank you! Found a custom function that removes \r from the textfile.

Best.
Topic archived. No new replies allowed.