(Qt) 文件读写基础
创始人
2024-12-16 17:05:09
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 console  SOURCES += \         main.cpp 

main.cpp

#include  #include  #include   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

相关内容

热门资讯

热点推荐!创思维激k辅助工具,... 热点推荐!创思维激k辅助工具,点星休闲辅助器下载,细节开挂辅助教程(真的有挂)1、下载安装好点星休闲...
透明讲解!wepoker透视脚... 透明讲解!wepoker透视脚本免费使用视频,uupoker透视(起初有开挂辅助下载);无需打开直接...
一分钟了解!腾威互娱破解辅助工... 一分钟了解!腾威互娱破解辅助工具,掌中乐游戏中心破解版,发现开挂辅助教程(的确有挂);无需打开直接搜...
透视黑科技!wepoker辅助... 您好:wepoker辅助是真的吗这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很...
透视安卓版!wepoker私人... 透视安卓版!wepoker私人局透视方法,wejoker辅助器怎么卖(起初有开挂辅助工具);无需打开...
今日科普!微乐小程序辅助开挂,... 今日科普!微乐小程序辅助开挂,河洛杠次插件,通报开挂辅助教程(有挂分享);无需打开直接搜索打开薇:1...
透视辅助!wepoker透视脚... 透视辅助!wepoker透视脚本免费使用视频,we poker辅助器v3.3(本然有开挂辅助工具);...
重大通报!指尖四川辅助破解版苹... 重大通报!指尖四川辅助破解版苹果,兴动互娱脚本,曝光开挂辅助教程(有挂透视);无需打开直接搜索薇:1...
透明了解!wepoker有辅助... 透明了解!wepoker有辅助器吗,hhpoker德州牛仔视频(往昔有开挂辅助器);无需打开直接搜索...
盘点几款!吉祥填大坑有插件吗,... 盘点几款!吉祥填大坑有插件吗,战神辅助器下载,科技开挂辅助教程(有人有挂)1、下载安装好吉祥填大坑有...