#include <QCoreApplication> #include <QHash> #include <QFile> #include <QDataStream> #include <QString> #include <QDebug> #include <iostream> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QHash<int, QString> hash; hash.insert(0, "working"); hash.insert(1, "hard"); hash.insert(2, "!"); QString str("this is a QDataStream test "); QFile file_out("test.dat"); if(!file_out.open(QIODevice::WriteOnly)) { std::cerr<<"can not open file test.dat:"<<qPrintable(file_out.errorString())<<std::endl; // return; } QDataStream out(&file_out); out.setVersion(QDataStream::Qt_4_8); out<<quint32(0x12345678)<<str<<hash; if(!file_out.flush()) { qDebug("write faile"); } QFile file_in("test.dat"); if(!file_in.open(QIODevice::ReadOnly)) { std::cerr<<"can not open file test.dat:"<<qPrintable(file_in.errorString())<<std::endl; } QDataStream in(&file_in); in.setVersion(QDataStream::Qt_4_8); quint32 c; QHash<int, QString> b; QString str1; in>> c >> str1 >> b; qDebug()<<c <<endl<<str1<<endl; QHash<int, QString>::const_iterator j; for(j = b.constBegin(); j != b.constEnd(); j++) qDebug()<<j.key()<<" "<<j.value()<<endl; return a.exec(); }