读取txt文件:
#include <QFile>
#include <QTextStream>
QFile file("filename.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
// 处理每一行数据
}
file.close();
写入txt文件:
#include <QFile>
#include <QTextStream>
QFile file("filename.txt");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return;
QTextStream out(&file);
out << "Hello World\n";
// 写入其他数据
file.close();