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();

}

相关推荐
minji...5 分钟前
Linux 网络套接字编程(一)端口号port,socket套接字,socket,bind,socket 通用结构体
linux·运维·服务器·开发语言·网络
2301_814809866 分钟前
踩坑实战pywebview:用 Python + Web 技术打造轻量级桌面应用
开发语言·前端·python
南境十里·墨染春水7 分钟前
C++流类库 字符串流
开发语言·c++
weixin_5206498730 分钟前
C#线程底层原理知识
java·开发语言
AC赳赳老秦34 分钟前
OpenClaw与Excel联动:批量读取/写入数据,生成可视化报表
开发语言·python·excel·产品经理·策略模式·deepseek·openclaw
code_whiter34 分钟前
C++9(vector)
开发语言·c++
覆东流36 分钟前
第5天:Python字符串操作进阶
开发语言·后端·python
吴梓穆37 分钟前
UE5 C++ 使C++创建动画蓝图
开发语言·c++·ue5
冰暮流星38 分钟前
javascript之表单事件1
开发语言·前端·javascript