ReadLine() is reading whole CSV file on Mac
-
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(); }
@
-
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/
-
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?).
-
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?
-
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.
-
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.