使用QTextStream向txt文件輸出換行時,需要使用QIODevice::Text標誌。
官方文檔對QIODevice::Text的解釋:
When reading, the end-of-line terminators are translated to '\n'. When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32.
簡而言之,該標誌指示在讀寫過程中要對end-of-line進行轉換。
例子:
(1)不使用QIODevice::Text
- #include <QCoreApplication>
- #include <QFile>
- #include <QTextStream>
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- QFile file("C:/Users/Administrator/Desktop/1.txt");
- if (file.open(QIODevice::WriteOnly))
- {
- QTextStream out(&file);
- out << endl << '*';
- }
- return a.exec();
- }
輸出:
可見輸出換行達不到效果。
(2)不使用QIODevice::Text
- #include <QCoreApplication>
- #include <QFile>
- #include <QTextStream>
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- QFile file("C:/Users/Administrator/Desktop/1.txt");
- if (file.open(QIODevice::Text | QIODevice::WriteOnly))
- {
- QTextStream out(&file);
- out << endl << '*';
- }
- return a.exec();
- }
效果:
轉自:http://blog.csdn.net/u012689588/article/details/19431353