I’m trying to read and print a file that may or may not contain UTF-8 codes.
My example file contains:
hi
\u22a2
hey
My niave code:
QFile file("/path/qteststream.txt");
if(!file.open(QIODevice::ReadOnly | QIODevice::Text ))
std::cout << "Fail" << std::endl;
QTextStream stream(&file);
QTextStream qout(stdout);
stream.setAutoDetectUnicode(true);
while(!stream.atEnd())
qout << stream.readLine() << endl;
But obviously, Im doing something wrong. I’d expect \u22a2 to be printed as a unicode character, but the programm just prints the string. How can I make print such sequences as characters?
\u22a2may be interpreted as a Unicode character by your C++ compiler, but things are different outside source code. In a text file,\u22a2is merely 6 characters (\,u,2,2,a, and2) in succession. Try pasting⊢directly into the text file.If you intentionally have
\u22a2in the text file and want to convert it into a Unicode character, you may want to check this post.