1、文本流设置unicode小端模式
2、写入文本前两个字节FF FE
3、字符串转成unicode编码
QList<QByteArray> list = QTextCodec::availableCodecs();//可以获取Qt支持的编码,下面列出字符串的几种编码名称//GBK GB2312//unicode 小端//UTF-8//UnicodeLitteUnmarked 小端//UnicodeBigUnmarked 大端#include <Qapplication>#include <QFile>#include <QTextStream>#include <QTextCodec>int main(int argc, char *argv[]){ QApplication a(argc, argv); // Widget w; // w.show(); QFile fielWrite("a.txt"); if (!fielWrite.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate)) { return 0; } QTextStream out(&fielWrite); out.setCodec(QTextCodec::codecForName("unicode"));//unicode小端模式 //out.setAutoDetectUnicode(true); //好像没用处 QChar head = 0xfeff;//unicode文件头 文本里前两个字节为FFFE out << head; QString str = "我";//"我"在Qt文本编辑器里默认是UTF-8编码格式,转成QString之后为unicode编码 out << str; fielWrite.close(); return a.exec();}
新闻热点
疑难解答