依赖的头文件:
#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();
- //注意写入方式的选择,注意写完后的关闭操作!
本文介绍如何利用Qt库中的QTextStream类来实现对文本文件的写入操作。示例代码展示了如何打开一个文件,以只写的方式,并通过QTextStream对象将字符串写入到文件中。
&spm=1001.2101.3001.5002&articleId=20734183&d=1&t=3&u=ccec1b64ad0e4f14bfab92c1248912ae)
235

被折叠的 条评论
为什么被折叠?



