C++程序设计之Qt基本数据类型的序列化与反序列化

一、Qt基本数据类型的序列化

    QFile file1("QText.dat");
    if(!file1.open(QIODevice::WriteOnly | QIODevice::Text)) return;
    QDataStream out(&file1);

    QString str1 = "中华人民共和国";
    int iData1 = 10000;
    double dData1 = 3.14;
    QVector<int> vec1 = {0,1,2,3,4,5,6,7,8,9};

    out << str1 << iData1 << dData1 << vec1;
    file1.close();

二、Qt基本数据类型的反序列化

    QFile file2("QText.dat");
    if(!file2.open(QIODevice::ReadOnly | QIODevice::Text)) return;
    QDataStream in(&file2);

    QString str2;
    int iData2;
    double dData2;
    QVector<int> vec2;

    in >> str2 >> iData2 >> dData2 >> vec2;
    qDebug() << str2 << iData2 << dData2 << vec2;
    file2.close();

Leave a Reply

Your email address will not be published.