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. ReadLine() is reading whole CSV file on Mac

ReadLine() is reading whole CSV file on Mac

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 5.1k 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.
  • F Offline
    F Offline
    fireking
    wrote on last edited by
    #1

    Cannot get readLine just to read to EOL. What am I doing wrong?

    @
    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
    QTextStream in(&file);

        while (!in.atEnd())
        {
            QString line = in.readLine();
            qDebug() << "reading line..." << line;
    
            processLineFromCSV(line);
        }
        file.close();
    }
    

    @

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Your code looks correct. I wonder if your CSV file is using non-Mac EOL encodings? Try these:

      • Remove "QIODevice::Text" from open(), OR
      • Open your file in a Mac text editor, modify it, and re-save it

      If either of those work, then you've found a bug in Qt. If you don't have the "latest versions":http://qt-project.org/downloads of Qt, upgrade and try again. If you have the latest but the issue is still there, please report it to https://bugreports.qt-project.org/

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Rondog
        wrote on last edited by
        #3

        According to what is in the Qt help says the end of line terminators used for QTextStream are either "\n" or "\r\n" which is Unix or Windows. The Mac end of line terminator is "\r".

        If you remove the 'QIODevice::Text' flag you can process the file without using QTextStream. If the file size is reasonable you could read everything into QString then substitute "\r\n" for "\n" and "\r" for "\n" then use QString.split("\n") to get the individual lines.

        I am surprised what you have doesn't work. I would have expected QTextStream would recognize a Mac line terminator. I have a Mac too and most of the files use Unix line terminators (I think?).

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          Mac line endings were a lone CR only until Mac OS X after which they adopted the UNIX-style LF only marker. Is this a very old file?

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fireking
            wrote on last edited by
            #5

            Works!

            I tried saving the file from Excel on the mac and it did not seem to help.

            Added \r\n to csv EOL and that works now. File was being generated in Java on Windows XP machine using newline() as a line terminator.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              ChrisW67
              wrote on last edited by
              #6

              Then somebody has gone out of their way to write oddball text files. BufferedWriter.newLine() uses the system property line.separator which almost certainly defaults to "\r\n" on Windows builds, and "\n" on UNIX/OS X builds: both of which would be handled by Qt.

              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