依赖的头文件:
#include <QtCore/QTextStream>
#include <QtCore/QFile>
#include <QtCore/QIODevice>
QString sFilePath = "C:\\test.txt";
QFile file(sFilePath);
//方式:Append为追加,WriteOnly,ReadOnly
if (!file.open(QIODevice::WriteOnly|QIODevice::Text)) {
QMessageBox::critical(NULL, "提示", "无法创建文件");
return -1;
}
QTextStream out(&file);
out<<"要写入内容"<<endl;
out.flush();
file.close();
//注意写入方式的选择,注意写完后的关闭操作!