如何通过Qt更改记事本的编码

如何通过Qt更改记事本的编码,qt,Qt,我正在通过Qt在记事本中打开一个文件,但记事本以错误的编码读取它。我会让图片来解释。。。(我在两幅图中圈出了编码) 这就是我得到的 这就是我想要的 如何强制记事本以正确的编码读取?在记事本中打开文件之前尝试修改文件-根据添加UTF-8标记 以下是代码(简化,在Windows中工作): #包括 #包括 int main(int/*argc*/,char*/*argv*/[]) { const QString notepath=“D:\\your file.bin”; 常量静态uint8_t u

我正在通过Qt在记事本中打开一个文件,但记事本以错误的编码读取它。我会让图片来解释。。。(我在两幅图中圈出了编码)

这就是我得到的

这就是我想要的


如何强制记事本以正确的编码读取?

在记事本中打开文件之前尝试修改文件-根据添加UTF-8标记

以下是代码(简化,在Windows中工作):

#包括
#包括
int main(int/*argc*/,char*/*argv*/[])
{
const QString notepath=“D:\\your file.bin”;
常量静态uint8_t utf8marker[]={0xEF,0xBB,0xBF};
QFile文件(notepath);
打开(QIODevice::ReadWrite);
自动缓冲区=file.readAll();
buffer.prepend(reinterpret_cast(utf8marker),sizeof(utf8marker));
查找文件(0);
文件写入(缓冲区);
file.close();
QProcess::StartDetailed(“C:\\Windows\\system32\\notepad.exe,{notepath});
返回0;
}

在记事本中打开文件之前尝试修改文件-根据添加UTF-8标记

以下是代码(简化,在Windows中工作):

#包括
#包括
int main(int/*argc*/,char*/*argv*/[])
{
const QString notepath=“D:\\your file.bin”;
常量静态uint8_t utf8marker[]={0xEF,0xBB,0xBF};
QFile文件(notepath);
打开(QIODevice::ReadWrite);
自动缓冲区=file.readAll();
buffer.prepend(reinterpret_cast(utf8marker),sizeof(utf8marker));
查找文件(0);
文件写入(缓冲区);
file.close();
QProcess::StartDetailed(“C:\\Windows\\system32\\notepad.exe,{notepath});
返回0;
}

但这是两个不同的文件。。。编码不是这里的主要问题,第二个文件与第一个文件相同,但使用一个程序打开,该程序将其作为临时文件打开,但这是两个不同的文件。。。编码不是这里的主要问题。第二个文件与第一个文件相同,但使用一个程序打开,该程序将其作为临时文件打开
#include <QFile>
#include <QProcess>

int main(int /*argc*/, char */*argv*/[])
{
    const QString notepath = "D:\\your-file.bin";
    const static uint8_t utf8marker[] = {0xEF, 0xBB, 0xBF};

    QFile file(notepath);
    file.open(QIODevice::ReadWrite);
    auto buffer = file.readAll();

    buffer.prepend(reinterpret_cast<const char*>(utf8marker), sizeof(utf8marker));

    file.seek(0);
    file.write(buffer);
    file.close();

    QProcess::startDetached("C:\\Windows\\system32\\notepad.exe", {notepath});
    return 0;
}