How to read UTF-8 text from file using Qt?
18,379
Solution 1
in.setCodec("UTF-8");
Solution 2
You shuold do:
QTextStream in(&file);
in.setCodec("UTF-8"); // change the file codec to UTF-8.
while(!in.atEnd())
{
QString line = in.readLine();
qDebug() << line.toLocal8Bit(); // convert to locale multi-byte string
}
Solution 3
I was also having ????
when reading an XML file with QXmlStreamReader
. I fixed it by calling this before reading the file:
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
Related videos on Youtube
Author by
Maciej Ziarko
Updated on October 11, 2020Comments
-
Maciej Ziarko over 3 years
I have some problems with reading UTF-8 encoded text from file. My version reads only ASCII characters.
#include <QtCore> int main() { QFile file("s.txt"); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { return -1; } QTextStream in(&file); while(!in.atEnd()) { QString line = in.readLine(); qDebug() << line; } }
s.txt:
jąkać się ślimak śnieżyca
output:
"jka si" "limak" "nieyca"
What should I use?
-
Maciej Ziarko about 13 yearsI tried it and now all the non-ASCII characters are replaced with "?".
-
Maciej Ziarko about 13 yearsAfter it I get: "j?ka? si?" "?limak" "?nie?yca"
-
edA-qa mort-ora-y about 13 yearsPerhaps your qDebug output console doesn't support those characters. That'd be a reasonable source of the "?". Try showing the string in a QLabel on-screen.
-
Maciej Ziarko about 13 yearsIt's not the issue. dDebug() << QString("śnieżyca"); works perfectly.
-
John Flatness about 13 yearsYou're sure your input file is actually UTF-8?
-
Maciej Ziarko about 13 years4 text editors (Geany, jEdit, gedit, Kate) say it is UTF-8. And still I have my problem with "?".
-
Yi Zhao about 13 yearsWhat's your default locale? If
qDebug() << QString("śnieżyca");
works perfectly, it means your system configured a non-ASCII locale. You should tryqDebug() << line.toLocal8Bit();
-
Maciej Ziarko about 13 yearsOK, it eventually fixed my problem. Thanks!