阅读量:0
文章目录
🗂️前言
📄ref
本文资料和测试版本为
Qt 5.15
📄访问标记
🗃️enum 标记
class QIODevice; enum OpenModeFlag { NotOpen = 0x0000, ReadOnly = 0x0001, WriteOnly = 0x0002, ReadWrite = ReadOnly | WriteOnly, Append = 0x0004, Truncate = 0x0008, Text = 0x0010, Unbuffered = 0x0020, NewOnly = 0x0040, ExistingOnly = 0x0080 }; Q_DECLARE_FLAGS(OpenMode, OpenModeFlag)
class QFileDevice; enum FileError { NoError = 0, ReadError = 1, WriteError = 2, FatalError = 3, ResourceError = 4, OpenError = 5, AbortError = 6, TimeOutError = 7, UnspecifiedError = 8, RemoveError = 9, RenameError = 10, PositionError = 11, ResizeError = 12, PermissionsError = 13, CopyError = 14 }; enum FileTime { FileAccessTime, FileBirthTime, FileMetadataChangeTime, FileModificationTime }; enum Permission { ReadOwner = 0x4000, WriteOwner = 0x2000, ExeOwner = 0x1000, ReadUser = 0x0400, WriteUser = 0x0200, ExeUser = 0x0100, ReadGroup = 0x0040, WriteGroup = 0x0020, ExeGroup = 0x0010, ReadOther = 0x0004, WriteOther = 0x0002, ExeOther = 0x0001 }; Q_DECLARE_FLAGS(Permissions, Permission) enum FileHandleFlag { AutoCloseHandle = 0x0001, DontCloseHandle = 0 }; Q_DECLARE_FLAGS(FileHandleFlags, FileHandleFlag)
🗂️Code
📄demo
qmake
QT += core #DESTDIR = $$PWD/bin CONFIG += c++11 console SOURCES += \ main.cpp
main.cpp
#include <QDebug> #include <QFile> #include <QString> void check_qfile(QFile& file) { qWarning() << file.error() << file.errorString(); } void file_write(QString file_path) { QFile file(file_path); if (file.open(QFile::WriteOnly) == false) { qDebug() << __func__; check_qfile(file); return; } QString name = "cuber-lotus"; QString url = "https://space.bilibili.com/8172252"; /// QTextStream &endl(QTextStream &s) QTextStream(&file) << name << Qt::endl; /// QString -> QByteArray file.write(url.toLocal8Bit()); qDebug() << __func__; check_qfile(file); file.close(); } void file_read(QString file_path) { QFile file(file_path); if (file.open(QFile::ReadOnly) == false) { qDebug() << __func__; check_qfile(file); return; } /// 直接读取全部 /// QByteArray -> QString QByteArray byteArr = file.readAll(); QString msg = byteArr; qDebug().noquote() << msg; qDebug() << __func__; check_qfile(file); file.close(); } int main() { QString file_path = "example.txt"; file_write(file_path); file_read(file_path); }
📄分点讲解
🗃️继承体系
QIODevice
是否继承于QObject
取决于是否指定(编译时指定)。
#################### #### QIODevice ### #################### | V #################### #### QFileDevice ### #################### | V #################### #### QFile ### ####################
🗃️打开/关闭
QFile file(file_path); if (file.open(QFile::WriteOnly) == false) { return; }
🗃️写
法1:
// 借助 QTextStream qt的文件流操作 QString name = "cuber-lotus"; /// QTextStream &endl(QTextStream &s) QTextStream(&file) << name << Qt::endl;
法2:
// 使用自带的 write QString url = "https://space.bilibili.com/8172252"; /// QString -> QByteArray file.write(url.toLocal8Bit());
qint64 write(const char *data, qint64 len); qint64 write(const char *data); inline qint64 write(const QByteArray &data) { return write(data.constData(), data.size()); }
🗃️读
- read
- reanLine
- readAll
qint64 read(char *data, qint64 maxlen); QByteArray read(qint64 maxlen); QByteArray readAll(); qint64 readLine(char *data, qint64 maxlen); QByteArray readLine(qint64 maxlen = 0); virtual bool canReadLine() const;
🗂️END
🌟关注我
关注我,学习更多C/C++,算法,计算机知识
B站:
👨💻主页:天赐细莲 bilibili