Qt基础开发之Qt文件操作类QFile读写文件的详细方法与实例及QDataStream的使用方法 Qt基础开发中的文件操作是非常重要的,Qt提供了QFile类来实现文件的读写操作。QFile类继承至QIODevice,QIODevice是输入/输出设备的基类,为设备提供了公共实现和抽象接口用于读写块数据。QIODevice又继承至QObject。 一、使用QFile类打开文件 QFile类的构造函数为QFile(const QString &name),它传入一个文件路径,但是在构造完成后,并没有打开文件,需要使用QFile::open函数来打开文件。open函数的参数是OpenMode mode,是一个枚举类型,包括: * QIODevice::NotOpen 不打开 * QIODevice::ReadOnly 只读方式 * QIODevice::WriteOnly 读写方式 * QIODevice::ReadWrite 读写方式 * QIODevice::Append 追加方式 * QIODevice::Truncate 阶段方式 * QIODevice::Text 转换不同平台的换行,读的时候把所有换行转成'\n',写的时候再把'\n'转换对应平台的换行 * QIODevice::Unbuffered 不使用缓冲区 例如:QFile file("d:/123.txt"); file.open(QIODevice::ReadOnly); 二、QFile类关闭文件 QFile类关闭文件使用close函数,close函数会刷新缓冲区,并关闭文件。 三、QFile类文件读操作 QFile类文件读操作使用QIODevice::read函数,read函数读取maxSize个字节,内部位置指针后移maxSize,并返回一个QByteArray对象。 例如:QFile file("d:/123.txt"); file.open(QIODevice::ReadOnly); qDebug() << file.read(10) << endl; file.close(); QFile类也提供了readLine函数,readLine函数读取一行,但是这一行不能超过maxSize字节,maxSize = 0代表不限制行字节数。 例如:QFile file("d:/123.txt"); file.open(QIODevice::ReadOnly); qDebug() << file.readLine(10) << endl; file.close(); QFile类也提供了readAll函数,readAll函数读取整个文件的内容。 四、QFile类文件写操作 QFile类文件写操作使用QIODevice::write函数,write函数将byteArray写入文件,写完内部位置指针后移。 例如:QFile file("d:/123.txt"); file.open(QIODevice::ReadWrite | QIODevice::Text); QByteArray byte("hellworld"); file.write(byte); file.write(byte); file.close(); 五、QDataStream流控文件输入输出 QDataStream流控文件输入输出可以使用QDataStream。流控写入代码如下: #include <QDataStream> #include <QDebug> #include <QFile> int main(int argc, charargv){ QFile file("d:/123.txt"); file.open(QIODevice::ReadWrite); QDataStream stream(&file); int a = 10; QString str = "helloworld"; stream << a << str; file.close(); return 0; } 流控读取代码如下: #include <QDataStream> #include <QDebug> #include <QFile> int main(int argc, charargv){ QFile file("d:/123.txt"); file.open(QIODevice::ReadOnly); QDataStream stream(&file); int a; QString str; stream >> a >> str; qDebug() << a << str; file.close(); return 0; } Qt基础开发中的文件操作类QFile提供了详细的读写文件的方法与实例,并且可以使用QDataStream流控文件输入输出。
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/release/download_crawler_static/12727796/bg1.jpg)
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
- 粉丝: 0
- 资源: 897
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
- 打包和分发Rust工具.pdf
- SQL中的CREATE LOGFILE GROUP 语句.pdf
- C语言-leetcode题解之第172题阶乘后的零.zip
- C语言-leetcode题解之第171题Excel列表序号.zip
- C语言-leetcode题解之第169题多数元素.zip
- ocr-图像识别资源ocr-图像识别资源
- 图像识别:基于Resnet50 + VGG16模型融合的人体细胞癌症分类模型实现-图像识别资源
- C语言-leetcode题解之第168题Excel列表名称.zip
- C语言-leetcode题解之第167题两数之和II-输入有序数组.zip
- C语言-leetcode题解之第166题分数到小数.zip
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)