I have a string in KOI8-R encoding, it’s passed as argv, so initially it’s a char* object. Using this deprecated encoding can’t be helped at this point, unfortunately. The system locale is KOI8-R, and the source code is in this encoding too.
The string is a path to a file where some of the directories have names in KOI8-R. I need to open the file to write using QFile. However, open() always returns false no matter what I try.
I need to convert the path to QString to pass it to the constructor of QFile, however, straightforward conversion, QString::fromLocal8Bit() and QFile::decodeName() don’t seem to help.
char* filename; // This is a KOI8-R string
QFile f(QString::fromLocal8Bit(filename));
// QFile f(QFile::decodeName(filename)) doesn't work too
f.open(QIODevice::WriteOnly | QIODevice::Text); // returns false
The only way I made it work was with the help of ofstream objects from STL, passing char* to ofstream constructor works fine, however, the use of QFile is much more preferable as the application is in QT.
The solution to this was to add QApplication initialization (QApplication a(argc, argv);) BEFORE doing anything with the strings. Apparently locale initialization is somewhere deep inside QApplication constructor.