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?
See QTextStream::setCodec()
:
in.setCodec("UTF-8");
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With