Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Mac: getline not working as it should (or as it does in Windows)
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    igorland
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • I Offline
        I Offline
        igorland
        wrote on last edited by
        #3

        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!

        1 Reply Last reply
        0
        • I Offline
          I Offline
          igorland
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • S Offline
            S Offline
            steno
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • I Offline
                I Offline
                igorland
                wrote on last edited by
                #7

                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!

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  You should use in.atEnd()

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved