网站首页 > 博客文章 正文
每一种编程语言都会对文件进行操作,有读有写,Qt也提供了对文件的读写操作,提供了下、两种形式的纯文本文件操作方式:
- 用 QFile 类的 IODevice 读写功能直接进行读写
- 利用 QFile 和 QTextStream 结合起来,用流(Stream)的方法进行文件读写
下面对两种方式以代码的形式给大家说说怎么用:
QFile读写文本文件
//读取纯文本文件内容
bool readFile()
{
QString aFileName= "D:\test.txt";
QFile aFile(aFileName);
if (!aFile.exists()) //文件不存在
return false;
if (!aFile.open(QIODevice::ReadOnly | QIODevice::Text))
return false;
//str就是文件内容
QString str = aFile.readAll();
aFile.close();
return true;
}
//把文件内容保存到文件中
bool saveFile()
{
QString aFileName= “D:\test.txt";
QFile aFile(aFileName);
if (!aFile.open(QIODevice::WriteOnly | QIODevice::Text)))
return false;
QString str="abc";//整个内容作为字符串
QByteArray strBytes=str.toUtf8();//转换为字节数组
//QByteArray strBytes=str.toLocal8Bit();
aFile.write(strBytes,strBytes.length()); //写入文件
aFile.close();
return true;
}
QFile::open() 函数打开文件时需要传递 QIODevice::OpenModeFlag 枚举类型的参数,决定文件以什么方式打开,QIODevice::OpenModeFlag 类型的主要取值如下:
- QIODevice::ReadOnly:以只读方式打开文件,用于载入文件。
- QIODevice::WriteOnly:以只写方式打开文件,用于保存文件。
- QIODevice::ReadWrite:以读写方式打开。
- QIODevice::Append:以添加模式打开,新写入文件的数据添加到文件尾部。
- QIODevice::Truncate:以截取方式打开文件,文件原有的内容全部被删除。
- QIODevice::Text:以文本方式打开文件,读取时“\n”被自动翻译为换行符,写入时字符串结束符会自动翻译为系统平台的编码,如 Windows 平台下是“\r\n”。
QFile和QTextStream结合读写文本文件
QTextStream 与 IO 读写设备结合,为数据读写提供了一些方便的方法的类,QTextStream 可以与 QFile、QTemporaryFile、QBuffer、QTcpSocket 和 QUdpSocket 等 IO 设备类结合使用。
//文本读取
bool openFileByStream(const QString &aFileName)
{
//用 QTextStream打开文本文件
QFile aFile(aFileName);
if (!aFile.exists()) //文件不存在
return false;
if (!aFile.open(QIODevice::ReadOnly | QIODevice::Text))
return false;
QTextStream aStream(&aFile); //用文本流读取文件
//str就是读取到的文件内容
QString str = aStream.readAll());
aFile.close();//关闭文件
return true;
}
//写入文件中
bool saveFileByStream(const QString &aFileName)
{
//用QTextStream保存文本文件
QFile aFile(aFileName);
if (!aFile.open(QIODevice::WriteOnly | QIODevice::Text))
return false;
QTextStream aStream(&aFile); //用文本流读取文件
QString str="abc"; //转换为字符串
aStream<<str; //写入文本流
aFile.close();//关闭文件
return true;
}
这就是Qt文件操作的基本过程,今天你学会了吗?
猜你喜欢
- 2024-09-08 Qt 贪吃蛇制作(含源码)(qt编写贪吃蛇)
- 2024-09-08 qt 提示“启动程序失败,路径或者权限错误?”解决方法
- 2024-09-08 电脑文件不小心删除了怎么办?两个方法教你秒恢复
- 2024-09-08 Qt——内存回收(qti内存)
- 2024-09-08 C++跨平台库QT学习 操作Excel(跨平台c++开发工具)
- 2024-09-08 Qt 工程 pro文件(qt工程文件.pro在哪儿找)
- 2024-09-08 Qt中的快捷键汇总(qtcreator快捷键)
- 2024-09-08 Qt中文乱码解决思路(vscode终端中文乱码怎么解决)
- 2024-09-08 Qt5+VS2015编程实例:下拉列表框QComboBox控件使用
- 2024-09-08 Qt编程进阶(87):基于HTTP协议的网络文件下载
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)