[Qt-interest] Non-latin characters in QString and char * on Windows

Konstantin Tokarev annulen at yandex.ru
Thu Mar 3 19:59:10 CET 2011


03.03.2011, 19:39, "Pavel Koshevoy" <pavel at aragog.com>:
> I would recommend using the wchar_t filename to open std::ifstream on
> Windows:
> http://msdn.microsoft.com/en-us/library/70bb7saf%28v=VS.90%29.aspx
>
> This avoids all the guesswork about what non-unicode encoding should be
> used for the filename.

Thank you very much for advice, it helped!

If anyone else is interested, here is my working cross-platform code:

#include <QtGui/QApplication>
#include <QtGui/QFileDialog>
#include <QDebug>

#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QString fileName = QFileDialog::getOpenFileName();
        ifstream ifs;
#ifdef Q_OS_WIN32
    ifs.open(fileName.toStdWString().c_str());
#else
    ifs.open(QFile::encodeName(fileName).data());
#endif
    if (!ifs)
      return 0;

    // Check file contents
    while(ifs) {
        string t;
        ifs >> t;
        cout << t << '\n';
    }
    cout << "OK" << endl;

    return 0;
}

-- 
Regards,
Konstantin



More information about the Qt-interest-old mailing list