View Full Version : cannot handle macintosh end-of-line
gaboo
22nd September 2006, 16:29
I don't know how to read a macintosh text file. The end of line is a character '13'. It's '10' on unixes, 10+13 on windows.
Here is the code I use :
QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text))
{
QMessageBox::warning(this, tr("Application"),
tr("Cannot read file %1:\n%2.")
.arg(fileName)
.arg(file.errorString()));
return;
}
QTextStream in(&file);
QString line;
line = in.readLine();
while (!line.isNull())
{
doneWords.append(line);
line = in.readLine();
}
The readline() function reads the whole file if it uses 13 as a newline char.
How can I set the endline char beeing '13' ?
Thank you !
high_flyer
22nd September 2006, 16:58
Try this:
//line = in.readLine();
while (!in.atEnd()) {
line = in.readLine();
doneWords.append(line);
}
gaboo
22nd September 2006, 17:46
Thanks for your suggestion.
Doesn't work either. The problem is that Qt doesn't know that it's a mac style end-of-line.
I read a bit of Qt'sources and it seams that only windows and unix text handling is present.
I hope i'm wrong, but I fear I'm not.
I'll have to do this by hand, but don't know how.
It's a mater of changing every char(13) to char(10) and then load the file with a QTextStream.
gaboo
27th September 2006, 16:52
Up!
No more suggestion ?
Strange that I find nothing about this.
I must be missing something.
jacek
27th September 2006, 16:55
If those files aren't too long, you could try QString::split().
gaboo
27th September 2006, 17:00
Sadly, they can be rather large : from 80mb up to 500mb.
I'm starting doing it manually. If I find a better way using QTextStream, I'll be glad to update my code :)
jacek
27th September 2006, 17:36
Then maybe std::getline() (http://www.cppreference.com/cppstring/getline.html) will do?
gaboo
27th September 2006, 18:14
Then maybe std::getline() (http://www.cppreference.com/cppstring/getline.html) will do?
I'll have a look at it, thanks.
gaboo
28th September 2006, 14:59
Answer from someone on qt-interest mailing list :
Unfortunately, this is a known bug. But the Trolls do not seem to care that
the Mac folk have lots of old text files to read. (The line ending on Mac OS
X is now LF, not CR.)
You will have to implement your own readLine, or fix Qt's.
Of course, the _right_ way is to check for LF, CR, or LF/CR for every line.
Making any assumptions based on the platform or first line is dangerous.
(For instance, Visual Studio will create mixed line ending files when you
edit a different line ending type than the default. Edited lines will have
the default, but other lines retain the original.)
This has been a problem for years that is so easily solved. I fail to
understand why some developers still try to force everyone else to use their
line ending choice. It is such a Microsoft attitude.
Keith
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.