Qt操作txt文本文件

一、清空文本内容

QString text_path("G:\camSim_20210917\Library\G_codes.txt");

QFile file(text_path);

file.open(QFile::WriteOnly | QFile::Truncate);

file.close();

二、读取txt文件,按行读

void ReadWriteTxt::readTxt()

{

//读取文件

QFile file("./account.txt");

QTextStream stram(&file); //纯文本文件流

if(file.open(QFile::ReadOnly | QFile::Text))

{

while (!stram.atEnd())

{

QString line = stram.readLine();

}

file.close();

}

}

三、写入

void ReadWriteTxt::writeTxt()

{

QFile file(m_strTxtPath);

if(!file.open(QIODevice::WriteOnly |QIODevice::Text))

return;

QTextStream stream(&file);

stream << "111" << "练习" <<"\n";

stream << "222" << "练习" <<"\n";

file.close();

}

相关推荐
踩着两条虫5 小时前
「AI + 低代码」的可视化设计器
开发语言·前端·低代码·设计模式·架构
JoneBB5 小时前
ABAP Webservice连接
运维·开发语言·数据库·学习
即使再小的船也能远航5 小时前
【Python】安装
开发语言·python
Irissgwe6 小时前
类与对象(三)
开发语言·c++·类和对象·友元
雪度娃娃6 小时前
转向现代C++——优先选用nullptr而不是0和NULL
开发语言·c++
萌新小码农‍7 小时前
python装饰器
开发语言·前端·python
KK溜了溜了7 小时前
Python从入门到精通
服务器·开发语言·python
故事和你917 小时前
洛谷-【图论2-1】树5
开发语言·数据结构·c++·算法·动态规划·图论
threelab7 小时前
Three.js 初中数学函数可视化 | 三维可视化 / AI 提示词
开发语言·前端·javascript·人工智能·3d·着色器
xiaoshuaishuai88 小时前
C# CDN加速与离线包优化PowerSetting慢问题
开发语言·windows·spring·c#