我最近想学习qt如何对文件进行编码和解码。我测试了一些例子,但有些让我感到困惑。
这是我的代码:
#include <QApplication>
#include <QFile>
#include <QTextStream>
#define TEST(testFile) void test##testFile()
TEST(ANSI)
{
QFile inFile(":/test/test-ANSI.txt");
if (!inFile.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QFile outFile("../test-ANSI-bak.txt");
if (!outFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
return;
QTextStream in(&inFile);
QTextStream out(&outFile);
out.setCodec("UTF-8");
// è¿æ ·åå
¥ä¼å»ææ¢è¡ç¬¦å·
while (!in.atEnd()) {
QString line = in.readLine();
out << line;
}
}
TEST(GB2312)
{
QFile inFile(":/test/test-GB2312.txt");
if (!inFile.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QFile outFile("../test-GB2312-bak.txt");
if (!outFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
return;
QTextStream in(&inFile);
QTextStream out(&outFile);
out.setCodec("UTF-8");
while (!in.atEnd()) {
QString line = in.readLine();
out << line;
}
}
TEST(utf8BOM)
{
QFile inFile(":/test/test-utf8-BOM.txt");
if (!inFile.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QFile outFile("../test-utf8-BOM-bak.txt");
if (!outFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
return;
QTextStream in(&inFile);
QTextStream out(&outFile);
out.setCodec("UTF-8");
while (!in.atEnd()) {
QString line = in.readLine();
out << line;
}
}
TEST(utf8)
{
QFile inFile(":/test/test-utf8.txt");
if (!inFile.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QFile outFile("../test-utf8-bak.txt");
if (!outFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
return;
QTextStream in(&inFile);
QTextStream out(&outFile);
out.setCodec("UTF-8");
while (!in.atEnd()) {
QString line = in.readLine();
out << line;
}
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
testANSI();
testGB2312();
testutf8BOM();
testutf8();
return app.exec();
}
我的输入文本内容如下:
----
I love you!
æç±ä½ ï¼
ããã
1234
1234
对于ANSI、GB2312、utf8 BOM,我得到了输出文件
例如:测试ANSI bak。txt文件
----I love you!æç±ä½ ï¼ããã12341234
对于utf8,我得到了输出文件
例如:test-utf8-bak。txt文件
----I love you!é´æ åæµ£ç
ç´éåâ¬åâ¬12341234
为什么test-utf8-bak。txt显示凌乱的代码?我很困惑!