使用文件、cp1251 和 utf-8 编码

Working with files, cp1251 and utf-8 encodings

现在我有类似的东西

QCoreApplication a(argc, argv);

QStringList argList = a.arguments();

if (argList.length() < 2 || argList.length() > 3)
{
    QTextStream(stdout) << "Usage: toGift input_file [output_dir]\n";
    return - 1;
}

用于解码 Windows-1251 个字符串,但我仍然得到错误的文件名,它是通过 argv 传递的。文件名是西里尔字母。无法弄清楚我错过了什么

Windows 控制台应用程序中的文本编码是一件棘手的事情。提示:使用 QApplication 的 arguments() 成员(QCoreApplication,如果它是控制台可执行文件),它直接从 Windows 以 UTF-16 格式检索参数,而不尝试从用于 [=] 的有损本地编码对其进行解码12=]。

最后是这样的:

QTextCodec *codec = QTextCodec::codecForName("Windows-1251");
if(codec == 0)
{
    QTextStream(stdout) << "No codec was found" << endl;
}
QTextDecoder *decoder = codec->makeDecoder();

QString inp_fileName = QString(decoder->toUnicode(argv[1]));
................
QFile inp_file(inp_fileName);
if(!inp_file.open(inp_file.ReadOnly))
{
    QTextStream(stdout) << "Could not open file for reading.\n" << inp_file.errorString() << endl;
    return -3;
}

我还需要解码器进一步使用。