分享

Qt读写二进制文件

 niudp 2015-07-14

下面是简单的二进制读写示例

  1. #include <QtCore/QCoreApplication>  
  2. #include <QFile>  
  3. #include <QByteArray>  
  4. #include <iostream>  
  5. using namespace std;  
  6. int main(int argc, char *argv[])  
  7. {  
  8.     QCoreApplication a(argc, argv);  
  9.     QFile file("test.dat");  
  10.     file.open(QIODevice::WriteOnly);  
  11.     double _a = 13.95;  
  12.     int b = 189;  
  13.     file.write((char*)&_a, sizeof(_a));  
  14.     file.write((char*)&b, sizeof(b));  
  15.     file.close();  
  16.   
  17.     QFile tmpfile("test.dat");  
  18.     tmpfile.open(QIODevice::ReadOnly);  
  19.     double c = 0;  
  20.     int d = 0;  
  21.     tmpfile.read((char*)&c, sizeof(c));  
  22.     tmpfile.read((char*)&d, sizeof(d));  
  23.     cout<<c<<' '<<d<<endl;  
  24.     //QByteArray bytes = tmpfile.readAll();  
  25.     //c = *((double*)bytes.data());  
  26.     //d = *((int*)(bytes.data() + sizeof(c)));  
  27.     //cout<<c<<' '<<d<<endl;  
  28.     tmpfile.close();  
  29.   
  30.     return a.exec();  
  31. }  


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多