[Solved] The correct way to read a file line by line
I've read an infinite count of posts and the documentation, but still, I've not managed to make it work. This is my code:
file.open(QIODevice::ReadOnly | QIODevice::Text);
in.setCodec("UTF-8"); // change the file codec to UTF-8.
QString line = in.readLine();
qDebug() << line;
//OR qDebug() << line.toLocal8Bit; None of these ways work
I'm not very sure about in.setCodec("UTF-8"); but I know that UTF-8 accepts special characters that I want, like Greek, Chinese, Arabic, French with tones etc.
I just want to read the contents of any text file without the concern of special characters...
Well, I do not remember using QTextStream::setCodec for some Greek strings I had used! Instead, if I recall correctly, I had used QString::fromWCharArray for each one of the respective strings.
Hello Stavros, thanks for the answer; can you please post some lines of code just like I did in my original post so as to figure out what you mean?
[quote author="alexandros" date="1342202344"]Hello Stavros, thanks for the answer; can you please post some lines of code just like I did in my original post so as to figure out what you mean?[/quote]
Well, I found the source code I had written, and I actually had used a QDataStream, not a QTextStream.
However, in your example, now that I think of it, my previous suggestion will not help you, unless you are trying to WRITE to a file a string literal. Example (for assignement to a QString variable):
@QString testText = QString::fromWCharArray(L"Δοκιμή");@
So, to analyze your problem, what is the exact error you get with the code you initially posted? How did you create the .txt file that you use? Which Qt version on what platform (Operating System)?
I'm in ubuntu 12.04 and the about dialog of my qtcreator says:
Qt Creator 2.4.1
Based on Qt 4.8.0 (32 bit)
But, why does it matter how I've written to the file? I think that, no matter how I've written to the file, I should be able to read it all through, passing the special characters as well.
I don't mind using QDataStream, after all.
In this occassion, I've written the file using Gedit, but I'd like to be able to read from any text file.
I'm not getting any errors, but when I do a
qDebug() << line;
all I get is the latin characters, some symbols and the numbers, it is seems that it omits all the special characters.
Just a guess: Why don't you output to a QWidget (which is capable of handling UTF-8) like a QLabel? I am not sure if qDebug supports it.
Hah, you are right -> http://i.imgur.com/ownx6.png <-
The thing is, I am planning to store each line of the input_file inside a QStringList, there shouldn't be a problem reading from there. Should it?
Nice! Since your question was successfully answered, please edit the title of this thread so that it begins with [SOLVED]
[quote author="Stavros" date="1342209242"]Nice! Since your question was successfully answered, please edit the title of this thread so that it begins with [SOLVED][/quote]
That should be automatic ;)
bq. That should be automatic ;)
It should, but it's not. I think there's a feature request floating around for it in the bugtracker.
bq. The thing is, I am planning to store each line of the input_file inside a QStringList, there shouldn’t be a problem reading from there. Should it?
Shouldn't at all. QStrings are very unicode-friendly.
[quote author="mlong" date="1342211423"]Shouldn't at all. QStrings are very unicode-friendly.[/quote]
Nice. True, I've already tested it and you are correct!