(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

相关内容

热门资讯

据权威媒体报道!hhpoker... 据权威媒体报道!hhpoker是正品吗,易趣麻将辅助器,好像是真的有辅助软件(哔哩哔哩)运易趣麻将辅...
有挂透视!epoker透视底牌... 有挂透视!epoker透视底牌,pokernow辅助控制(透视)确实是有辅助工具(哔哩哔哩)1、许多...
透视方案!wepoker公共底... 透视方案!wepoker公共底牌(透视)一贯真的有辅助平台(哔哩哔哩)该软件可以轻松地帮助玩家将we...
分享一款!!微信小程序黑科技(... 分享一款!!微信小程序黑科技(外挂),小程序微乐辅助软件好像有挂秘笈1、微信小程序黑科技脚本辅助下载...
围绕透视问题!pokemmo脚... 围绕透视问题!pokemmo脚本辅助下载,九九山城万州辅助,本来存在有辅助攻略(哔哩哔哩)1、该软件...
了解透视!约局吧德州真的有透视... 了解透视!约局吧德州真的有透视挂吗,来玩app破解(透视)切实一直总是有辅助技巧(哔哩哔哩)1)约局...
透视项目!约局吧德州可以透视吗... 透视项目!约局吧德州可以透视吗(透视)总是是有辅助平台(哔哩哔哩)1、透视项目!约局吧德州可以透视吗...
我来教大家!!微信小程序免费黑... 我来教大家!!微信小程序免费黑科技(外挂),微乐游戏竞技团辅助本来有挂方式1)微信小程序免费黑科技辅...
有了最新消息!拱趴大菠萝开挂方... 有了最新消息!拱趴大菠萝开挂方法,打两圈怀疑有外g挂,一直真的是有辅助教程(哔哩哔哩)1、完成拱趴大...
揭露透视!werplan有挂吗... 揭露透视!werplan有挂吗,大菠萝免费辅助(透视)原来一直总是有辅助教程(哔哩哔哩)在进入大菠萝...