Qt学习之路(56): 二进制文件读写 2010-04-11 17:29:39
QFlie | 访问本地文件系统或者嵌入资源 |
QTemporaryFile | 创建和访问本地文件系统的临时文件 |
QBuffer | 读写 QByteArray |
QProcess | 运行外部程序,处理进程间通讯 |
QTcpSocket | TCP 协议网络数据传输 |
QUdpSocket | 传输 UDP 报文 |
QSslSocket | 使用 SSL/TLS 传输数据 |
- QImage image("philip.png");
- QMap<QString, QColor> map;
- map.insert("red", Qt::red);
- map.insert("green", Qt::green);
- map.insert("blue", Qt::blue);
- QFile file("facts.dat");
- if (!file.open(QIODevice::WriteOnly)) {
- std::cerr << "Cannot open file for writing: "
- << qPrintable(file.errorString()) << std::endl;
- return;
- }
- QDataStream out(&file);
- out.setVersion(QDataStream::Qt_4_3);
- out << quint32(0x12345678) << image << map;
- quint32 n;
- QImage image;
- QMap<QString, QColor> map;
- QFile file("facts.dat");
- if (!file.open(QIODevice::ReadOnly)) {
- std::cerr << "Cannot open file for reading: "
- << qPrintable(file.errorString()) << std::endl;
- return;
- }
- QDataStream in(&file);
- in.setVersion(QDataStream::Qt_4_3);
- in >> n >> image >> map;
- QFile file("file.xxx");
- file.open(QIODevice::WriteOnly);
- QDataStream out(&file);
- // Write a header with a "magic number" and a version
- out << (quint32)0xA0B0C0D0;
- out << (qint32)123;
- out.setVersion(QDataStream::Qt_4_0);
- // Write the data
- out << lots_of_interesting_data;
- QFile file("file.xxx");
- file.open(QIODevice::ReadOnly);
- QDataStream in(&file);
- // Read and check the header
- quint32 magic;
- in >> magic;
- if (magic != 0xA0B0C0D0)
- return XXX_BAD_FILE_FORMAT;
- // Read the version
- qint32 version;
- in >> version;
- if (version < 100)
- return XXX_BAD_FILE_TOO_OLD;
- if (version > 123)
- return XXX_BAD_FILE_TOO_NEW;
- if (version <= 110)
- in.setVersion(QDataStream::Qt_3_2);
- else
- in.setVersion(QDataStream::Qt_4_0);
- // Read the data
- in >> lots_of_interesting_data;
- if (version >= 120)
- in >> data_new_in_XXX_version_1_2;
- in >> other_interesting_data;
本文出自 “豆子空间” 博客,请务必保留此出处http://devbean.blog.51cto.com/448512/293892