[Solved] The correct way to read a file line by line
-
wrote on 13 Jul 2012, 17:11 last edited by
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:
@
int main(){
QFile file("/home/alex/1.txt");
file.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream in(&file);
in.setCodec("UTF-8"); // change the file codec to UTF-8.
while(!in.atEnd())
{
QString line = in.readLine();
qDebug() << line;
//OR qDebug() << line.toLocal8Bit; None of these ways work
}
return 0;
}
@
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...
-
wrote on 13 Jul 2012, 17:23 last edited by
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.
-
wrote on 13 Jul 2012, 17:59 last edited by
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?
-
wrote on 13 Jul 2012, 19:21 last edited by
[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)?
-
wrote on 13 Jul 2012, 19:31 last edited by
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.
E.g.:
input_file:
@
PΠΠP
ΣΣWWΣΣ
@
will print:
@PP
WW@ -
wrote on 13 Jul 2012, 19:35 last edited by
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.
-
wrote on 13 Jul 2012, 19:49 last edited by
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?
-
wrote on 13 Jul 2012, 19:54 last edited by
Nice! Since your question was successfully answered, please edit the title of this thread so that it begins with [SOLVED]
-
wrote on 13 Jul 2012, 20:01 last edited by
[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 ;)
-
wrote on 13 Jul 2012, 20:30 last edited by
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.
-
wrote on 13 Jul 2012, 20:33 last edited by
[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!
6/11