当前位置: 首页> 教育> 锐评 > (Qt) 文件读写基础

(Qt) 文件读写基础

时间:2025/7/12 2:40:12来源:https://blog.csdn.net/CUBE_lotus/article/details/140621421 浏览次数:0次

文章目录

  • 🗂️前言
    • 📄ref
    • 📄访问标记
      • 🗃️enum 标记
  • 🗂️Code
    • 📄demo
    • 📄分点讲解
      • 🗃️继承体系
      • 🗃️打开/关闭
      • 🗃️写
      • 🗃️读
  • 🗂️END
    • 🌟关注我

🗂️前言

📄ref

  • Index of / (qt.io)

本文资料和测试版本为 Qt 5.15

📄访问标记

🗃️enum 标记

std::ios_base::openmode - cppreference.com

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 consoleSOURCES += \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 -> QByteArrayfile.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 -> QStringQByteArray 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

关键字:(Qt) 文件读写基础

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: