当前位置: 首页 > article >正文

QT----------文件系统操作和文件读写

一、输入输出设备类

  • 功能
    • Qt 提供了一系列的输入输出设备类,用于处理不同类型的 I/O 操作,如文件、网络等。

二、文件读写操作类

  • QFile 类
    • 提供了对文件的读写操作,可以打开、读取、写入和关闭文件。
    • 示例:
#include <QApplication>
#include <QFile>
#include <QTextStream>
#include <QDebug>


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);


    QFile file("example.txt");
    if (file.open(QIODevice::ReadWrite | QIODevice::Text)) {
        QTextStream stream(&file);


        // 写入数据
        stream << "Hello, World!" << endl;


        // 回到文件开头
        file.seek(0);


        // 读取数据
        QString line = stream.readLine();
        qDebug() << line;


        file.close();
    }


    return app.exec();
}
  • 代码解释
    1. 创建 QFile 对象并打开文件,使用 QIODevice::ReadWrite | QIODevice::Text 模式。
    2. 使用 QTextStream 进行文本的读写操作。
    3. stream << "Hello, World!" << endl; 写入文本。
    4. file.seek(0); 将文件指针移到开头。
    5. stream.readLine(); 读取一行文本。

三、QFileInfo 类

  • 功能
    • 提供了文件信息的访问,如文件大小、文件路径、文件是否存在等。
    • 示例:
#include <QApplication>
#include <QFileInfo>
#include <QDebug>


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);


    QFileInfo fileInfo("example.txt");


    if (fileInfo.exists()) {
        qDebug() << "File size: " << fileInfo.size();
        qDebug() << "File path: " << fileInfo.filePath();
    }


    return app.exec();
}
  • 代码解释
    1. 创建 QFileInfo 对象。
    2. 使用 exists() 检查文件是否存在。
    3. 使用 size() 获取文件大小,filePath() 获取文件路径。

四、QDir 类

  • 功能
    • 提供了对目录的操作,如创建目录、遍历目录、获取目录内容等。
    • 示例:
#include <QApplication>
#include <QDir>
#include <QDebug>


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);


    QDir dir;


    if (!dir.exists("new_dir")) {
        dir.mkdir("new_dir");
    }


    dir.setPath("new_dir");


    foreach (QFileInfo fileInfo, dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot)) {
        qDebug() << fileInfo.fileName();
    }


    return app.exec();
}
  • 代码解释
    1. 创建 QDir 对象。
    2. 使用 mkdir() 创建目录。
    3. 使用 entryInfoList() 遍历目录中的文件并输出文件名。
      在这里插入图片描述

五、QTemporaryDir 类

  • 功能
    • 创建临时目录。
    • 示例:
#include <QApplication>
#include <QTemporaryDir>
#include <QDebug>


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);


    QTemporaryDir tempDir;


    if (tempDir.isValid()) {
        qDebug() << "Temporary directory: " << tempDir.path();
    }


    return app.exec();
}
  • 代码解释
    1. 创建 QTemporaryDir 对象。
    2. 使用 isValid() 检查是否有效,path() 获取目录路径。

六、QTemporaryFile 类

  • 功能
    • 创建临时文件。
    • 示例:
#include <QApplication>
#include <QTemporaryFile>
#include <QTextStream>
#include <QDebug>


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);


    QTemporaryFile tempFile;


    if (tempFile.open()) {
        QTextStream stream(&tempFile);
        stream << "Temporary data";


        tempFile.seek(0);


        QString data = stream.readAll();
        qDebug() << data;
    }


    return app.exec();
}
  • 代码解释
    1. 创建 QTemporaryFile 对象并打开。
    2. 使用 QTextStream 进行读写操作。

七、QFileSystemWatcher 类

  • 功能
    • 监视文件和目录的更改。
    • 示例:
#include <QApplication>
#include <QFileSystemWatcher>
#include <QDebug>


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);


    QFileSystemWatcher watcher;


    watcher.addPath("example.txt");


    QObject::connect(&watcher, &QFileSystemWatcher::fileChanged, [](const QString &path) {
        qDebug() << path << " has changed";
    });


    return app.exec();
}
  • 代码解释
    1. 创建 QFileSystemWatcher 对象。
    2. 使用 addPath() 监视文件。
    3. 连接 fileChanged 信号,当文件更改时输出信息。

八、读文件和写文件

1. 用 QFile 读写文本文件
  • 参考前面 QFile 类的示例。
2. 用 QSaveFile 保存文件
  • QSaveFile 确保文件的原子性保存,避免写入失败时文件损坏。
#include <QApplication>
#include <QSaveFile>
#include <QTextStream>
#include <QDebug>


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);


    QSaveFile saveFile("example.txt");


    if (saveFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
        QTextStream stream(&saveFile);


        stream << "Hello, World!" << endl;


        if (saveFile.commit()) {
            qDebug() << "File saved successfully";
        } else {
            qDebug() << "Failed to save file";
        }
    }


    return app.exec();
}
  • 代码解释
    1. 创建 QSaveFile 对象并打开。
    2. 使用 QTextStream 写入数据。
    3. 使用 commit() 保存文件。
3. 读写二进制文件
  • 基础知识和工具软件

    • 可以使用 QDataStream 以预定编码方式读写文件,或使用原始二进制数据方式读写文件。
  • 使用 QDataStream 类读写二进制文件

#include <QApplication>
#include <QFile>
#include <QDataStream>
#include <QDebug>


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);


    QFile file("binary.dat");


    if (file.open(QIODevice::WriteOnly)) {
        QDataStream stream(&file);


        int number = 42;
        stream << number;


        file.close();
    }


    if (file.open(QIODevice::ReadOnly)) {
        QDataStream stream(&file);


        int readNumber;
        stream >> readNumber;


        qDebug() << "Read number: " << readNumber;


        file.close();
    }


    return app.exec();
}
  • 代码解释

    1. 打开文件,使用 QDataStream 写入和读取整数。
    2. stream << number; 写入数据。
    3. stream >> readNumber; 读取数据。
  • 使用原始二进制数据方式读写文件

#include <QApplication>
#include <QFile>
#include <QDebug>


int main(int argc, char *argv[])
{
    QApplication app(argc, argv);


    QFile file("raw.bin");


    if (file.open(QIODevice::WriteOnly)) {
        char data[] = {0x01, 0x02, 0x03};
        file.write(data, sizeof(data));
        file.close();
    }


    if (file.open(QIODevice::ReadOnly)) {
        char buffer[3];
        file.read(buffer, 3);


        qDebug() << "Read data: " << buffer[0] << buffer[1] << buffer[2];


        file.close();
    }


    return app.exec();
}
  • 代码解释
    1. 打开文件,使用 write() 写入原始二进制数据。
    2. 使用 read() 读取二进制数据。

通过上述各类文件系统操作和文件读写的示例,可以完成对文件和目录的多种操作,包括读写文本文件、二进制文件,以及使用临时文件和目录,同时可以监视文件系统的变化。你可以根据具体需求对这些代码进行修改和扩展,以满足不同的应用场景。

在这里插入图片描述


http://www.kler.cn/a/469732.html

相关文章:

  • Java解析PDF数据库设计文档
  • MyBatis 配置文件全解析
  • 发电厂冷水降电导 超纯水的制备和应用 抛光树脂
  • LLM - FlashAttention 的 Safe-Softmax 与 One-Pass Tiling 计算 教程
  • SolidWorks进行热力学有限元分析三、有限元计算
  • 【RISC-V CPU debug 专栏 4.1 -- RV Debug Vector Address 介绍】
  • 解决cryptoJS.AES默认参数加密,java无法解密的问题
  • 使用DockerCompose部署服务
  • 安装pnpm遇到的问题
  • 电脑提示wlanapi.dll丢失怎么办?wlanapi.dll丢失的多种解决方法
  • MySQL 锁那些事
  • 【数据结构与算法:七、查找】
  • ESP32-S3一款专为人工智能物联网打造的芯片
  • QT c++ 按钮 样式 设置按下和松开的背景颜色
  • Flink如何设置合理的并行度
  • 修改原生组件InlineDialog设置其下拉框支持滚动效果
  • x86霸权难动摇!
  • OpenGl(四) 提升Shader性能--VBO、EBO、VAO之VBO
  • Python版本变更历史及版本选择指南
  • 大风车excel:如何题库批量导入excel?批量导入excel