qt二进制文件读写修改


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    // Widget w;
    // w.show();

    //写入数据
    QString Path(QDir::currentPath() + QString("/test.bin"));
    // QFile modif_file(Path);
    // if(!modif_file.open(QIODevice::WriteOnly)){
    //     qDebug()<<"保存文件打开失败!";
    //     return -1;
    // }

    // QByteArray new_Data = QString("111").toUtf8();
    // qDebug()<<new_Data.size();

    // modif_file.write(new_Data);
    // modif_file.close();
    // qDebug()<<"写入完成!";

    //修改数据
    // QFile file(Path);
    // if(!file.open(QIODevice::ReadOnly | QIODevice::ReadWrite)){
    //     qDebug()<<"修改文件打开失败!";
    //     return -1;
    // }
    // QByteArray Data = file.readAll();
    // QByteArray new_Data = Data;
    // new_Data.prepend(QByteArray(3, '0'));  //修改数据

    QString resPath(QDir::currentPath() + QString("/res.bin"));
    // QFile file2(resPath);
    // if(!file2.open(QIODevice::ReadOnly | QIODevice::ReadWrite)){
    //     qDebug()<<"修改文件打开失败!";
    //     return -1;
    // }
    // file2.write(new_Data);
    // file2.close();
    // qDebug()<<"修改完成";

    //查看数据
    QFile file(resPath);
    if(!file.open(QIODevice::ReadOnly)){
        qDebug()<<"保存文件打开失败!";
        return -1;
    }
    QByteArray Data = file.readAll();      //读取原数据
    file.close();

    QString str(Data);
    qDebug()<<str;

    return a.exec();
}

Qt中进行二进制文件读写操作,可以使用`QFile`和`QDataStream`类。以下是一个完整的示例,展示了如何打开一个二进制文件并进行写入和读取操作。 ### 写入二进制文件 ```cpp #include <QFile> #include <QDataStream> #include <QDebug> void writeBinaryFile(const QString &filePath) { QFile file(filePath); if (!file.open(QIODevice::WriteOnly)) { qDebug() << "无法打开文件以写入:" << filePath; return; } QDataStream out(&file); // 写入一些基本数据类型 qint32 number = 123456789; double value = 3.14159265358979323846; QString text = "Hello, Qt Binary File!"; out << number << value << text; file.close(); qDebug() << "数据已成功写入到二进制文件"; } ``` ### 读取二进制文件 ```cpp #include <QFile> #include <QDataStream> #include <QDebug> void readBinaryFile(const QString &filePath) { QFile file(filePath); if (!file.open(QIODevice::ReadOnly)) { qDebug() << "无法打开文件以读取:" << filePath; return; } QDataStream in(&file); qint32 number; double value; QString text; in >> number >> value >> text; file.close(); qDebug() << "读取到的数据:"; qDebug() << "整数:" << number; qDebug() << "浮点数:" << value; qDebug() << "字符串:" << text; } ``` ### 使用示例 你可以在主函数或其他地方调用上述两个函数来测试二进制文件读写操作: ```cpp int main(int argc, char *argv[]) { QApplication app(argc, argv); QString filePath = "example.bin"; // 写入二进制文件 writeBinaryFile(filePath); // 读取二进制文件 readBinaryFile(filePath); return app.exec(); } ``` ### 解释 - **QFile**: 用于处理文件的打开、关闭和基本I/O操作。 - **QDataStream**: 提供了一种平台无关的方式来序列化C++的基本数据类型以及Qt的数据类型(如`QString`, `QByteArray`等)。它非常适合用于二进制文件读写[^1]。 - **QIODevice::WriteOnly** 和 **QIODevice::ReadOnly**: 这些标志指定了文件的操作模式。对于二进制文件,通常使用`QIODevice::WriteOnly`或`QIODevice::ReadOnly`,并且不需要添加`QIODevice::Text`标志,因为这会影响某些平台上的换行符转换。 ### 注意事项 - 在实际应用中,建议对读写操作进行错误检查,确保文件正确打开,并且数据能够顺利读取或写入。 - 如果需要处理大文件或者性能敏感的应用场景,可以考虑使用缓冲机制或者异步I/O操作。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值