• Qt: 读写二进制文件(写对象, 原始数据等)


    #include <iostream>
    #include <QFile>
    #include <QImage>
    #include <QMap>
    #include <QColor>

    class C {
    public:
    C(quint32 value = 0) :
    value(value) {
    }

    // Override operator << and >>.
    friend QDataStream &operator<<(QDataStream &out, const C &obj);
    friend QDataStream &operator>>(QDataStream &in, C &obj);

    quint32 getValue() const {
    return value;
    }

    private:
    quint32 value;
    };

    QDataStream &operator<<(QDataStream &out, const C &obj) {
    out << obj.value;

    return out;
    }

    QDataStream &operator>>(QDataStream &in, C &obj) {
    in >> obj.value;

    return in;
    }

    /**
    * Copy a file
    */
    bool copy(const QString &source, const QString &dest) {
    QFile sourceFile(source);
    if (!sourceFile.open(QIODevice::ReadOnly)) {
    #ifdef DEBUG
    std::cerr << sourceFile.errorString().toStdString() << std::endl;
    #endif
    return false;
    }

    QFile destFile(dest);
    if (!destFile.open(QIODevice::WriteOnly)) {
    #ifdef DEBUG
    std::cerr << destFile.errorString().toStdString() << std::endl;
    #endif
    return false;
    }

    destFile.write(sourceFile.readAll());

    return sourceFile.error() == QFile::NoError && destFile.error()
    == QFile::NoError;
    }

    /**
    * Instantiate a QFile
    * Open the file
    * Access the file through q QDataStream object.
    *
    * Must ensure that we read all the types in exactly the same order
    * as we wrote them.
    *
    * If the DataStream is being purely used to read and write basic C++ data types,
    * we dont' even need to call setVersion().
    *
    * If we want to read or write a file in one go. WE can avoid using QDataStream altogether
    * and instead using QIODevice's write() and readAll() function.
    * For example copy a file.
    */
    int main(int argc, char *argv[]) {
    //********Write data in to the file.********
    QImage image("Adium.png");
    QMap<QString, QColor> map;
    map.insert("red", Qt::red);
    map.insert("green", Qt::green);
    C c(23);
    QFile file("data.dat");
    if (!file.open(QIODevice::WriteOnly)) {
    std::cerr << "Cannot open the file: Write." << file.errorString().toStdString() << std::endl;

    return 1;
    }

    QDataStream out(&file);
    out.setVersion(QDataStream::Qt_4_3);
    out << quint32(7456) << map << c;
    file.close();

    //********Read data from the file.********
    quint32 value;
    QMap<QString, QColor> map2;
    C c2;
    if (!file.open(QIODevice::ReadOnly)) {
    std::cerr << "Cannot open the file: Read." << file.errorString().toStdString() << std::endl;

    return 2;
    }
    QDataStream in(&file);
    in.setVersion(QDataStream::Qt_4_3);
    in >> value >> map2 >> c2;
    file.close();

    std::cout << value << std::endl << c2.getValue();

    copy(QString("Adium.png"), QString("Copy_Adium.png"));

    return 0;
    }

    http://www.cppblog.com/biao/archive/2008/03/19/44810.html

  • 相关阅读:
    Apache Solr入门教程(初学者之旅)
    Codeforces 631 (Div. 2) E. Drazil Likes Heap 贪心
    Codeforces 631 (Div. 2) D. Dreamoon Likes Sequences 位运算^ 组合数 递推
    Codeforces 631 (Div. 2) C. Dreamoon Likes Coloring 思维or构造
    python中的类型转换
    MVC3.0在各个版本IIS中的部署
    get和post的区别
    Vue和React对比
    谈谈你对web标注和W3c的理解和认识
    js中的undefined 和null
  • 原文地址:https://www.cnblogs.com/findumars/p/5979181.html
Copyright © 2020-2023  润新知