Qt文件的读写操作
一、Qt文件读写基本描述
1.1 QFile实现文件的读写
QFile(const QString & name)
QFile(QObject *parent)
QFile(const QString & name, QObject *parent)
/*
1. 从QFile的构造函数我们知道,可以直接在调用构造函数的时候就传递文件名给QFile类
2. 对于先生成QFile对象然后调用setFileName()方法来设置文件,其中文件名中的路径分隔符要求必须是'/',其他分隔符QFile不支持。
*/
/*基本功能*/
copy() //复制文件
exists() //检查文件是否存在
open() //打开文件
remove() //删除文件
rename() //修改文件名
setFileName() //设置文件名
size() //文件大小
pos() //文件光标当前位置
seek() //设置文件光标位置
atEnd() //判断当前是否为文件尾
read() //读取文件内容
close() //关闭文件
1.2 QFile中关于QIODevice的宏定义
模式 | 值 | 描述 |
---|---|---|
NotOpen | 0x0000 | 不打开 |
ReadOnly | 0x0001 | 只读方式 |
WriteOnly | 0x0002 | 只写方式,不存在自建 |
ReadWrite | 0x0003 | 读写方式(ReadOnly|WriteOnly) |
Append | 0x0004 | 数据写入文件末尾 |
Truncate | 0x0008 | 打开文件前被截断,源数据丢失,覆盖 |
Text | 0x0010 | 读的时 |