I'm trying to create a UTF-8 coded file in Qt.
#include <QtCore>
int main()
{
QString unicodeString = "Some Unicode string";
QFile fileOut("D:\\Temp\\qt_unicode.txt");
if (!fileOut.open(QIODevice::WriteOnly | QIODevice::Text))
{
return -1;
}
QTextStream streamFileOut(&fileOut);
streamFileOut.setCodec("UTF-8");
streamFileOut << unicodeString;
streamFileOut.flush();
fileOut.close();
return 0;
}
I thought when QString is by default Unicode and when I set codec of the output stream to UTF-8 that my file will be UTF-8. But it's not, it's ANSI. What do I do wrong? Is something wrong with my strings? Can you correct my code to create UTF-8 file? Next step for me will be to read ANSI file and save it as UTF-8 file, so I'll have to perform a conversion on each read string but now, I want to start with a file. Thank you.