Mac: getline not working as it should (or as it does in Windows)



  • I naively thought that converting a project from Windows to Mac would be a very simple task. Not the case. Almost any piece of code that works well in Windows does not in OSX. Another issue:

    I have a text file that looks like this:

    [code]
    A|AAAAA|1|2
    R|RAAAA
    R|RAAAA

    A|BBBBB|1|2
    R|RBBBB
    R|RBBBB

    A|CCCCC|1|2
    R|RCCCC
    [/code]

    The following code searches for the relevant text in the file based on the key and returns all lines that belong to the key:

    [code]
    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;
    

    [/code]

    In Windows I get what I need:
    [code]
    A|BBBBB|1|2
    R|RBBBB
    R|RBBBB
    [/code]

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

    A|CCCCC|1|2
    R|RCCCC
    [/code]

    Whyyyyy????? :_((

    Cheers, everyone!


  • Lifetime Qt Champion

    Hi,

    Since you're using the std library rather than QFile, what does it have to do with Qt ?

    Note that line endings are not the same on Windows and OS X.



  • Greetings, SGaist!
    [quote author="SGaist" date="1388521382"]Hi,

    Since you're using the std library rather than QFile, what does it have to do with Qt ?

    [/quote]

    I guess the fact that it is done with QtCreator. Also, I could not find the correct equivalent for line.find() function with QTextStream.

    [quote author="SGaist" date="1388521382"]

    Note that line endings are not the same on Windows and OS X.[/quote]

    Huh? Thanks for that. I will google it.

    Best!



  • I am reading that '\n' IS the Mac's new line. I do not understand... Sorry...



  • Well windows uses line endings as '/r/n' and osx uses '/n'


  • Lifetime Qt Champion

    Then don't use QTextStream, just QFile to read your file line by line in a QByteArray or QString



  • OK. Before I go to celebrate the New Year, my last attempt failed. :-(

    [code]
    void MainWindow::on_pushButton_clicked()
    {
    QString navDataPath = "S://Path//File.txt";

    QFile navDataFile(navDataPath);
    
    if (navDataFile.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream in(&navDataFile);
    
    
        while(!navDataFile.atEnd() )
        {
    
            QString line = in.readLine();
    
            if(!line.contains("key"))
            {
                qDebug() << "NOT FOUND";
            }
    
            else
            {
    
                result = line + '\n';
    
                while (!navDataFile.atEnd() && !line.isNull())
                {
                    result += result + '\n';
                }
            }
        }
    }
    
    navDataFile.close();
    
    qDebug() << "LINE: " << result;
    

    }
    [/code]

    It crashes. I am at a loss.

    Happy New Year!


  • Lifetime Qt Champion

    You should use in.atEnd()


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.