#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

最新文章

  1. NOIP2012国王游戏
  2. ML 基础知识
  3. 一个ubuntu phper的自我修养(杂记)
  4. cocos2d-x 系列文章介绍
  5. JavaScript_解决safari浏览器window.open无法实现的问题
  6. keil采用C语言模块化编程时全局变量、结构体的定义、声明以及头文件包含的处理方法
  7. SKTransition类
  8. angular 中 directive中的多个指令
  9. vnc server配置、启动、重启与连接,图形管理linux系统
  10. 设置eclipse全局编码格式
  11. react-native不是内部或 外部命令,也不是可运行的程序或批处理文件
  12. Python高级特性(切片,迭代,列表生成式,生成器,迭代器)
  13. Python3基础系列-基本入门语法
  14. 软件安装配置笔记(二)——SQL Server安装
  15. go中的无限极分类的问题
  16. Window10中创建目录连接点
  17. 在Linux上安装SVN服务
  18. x86项目中读取注册表Register数据项的方法
  19. instancetype 与id
  20. javascript中对象和数组的异同点

热门文章

  1. docker &amp; nodejs
  2. 关于ajax解析
  3. Apache使用mysql认证用户
  4. jvmstat监控jvm内存
  5. JS如何获取iframe内html的body值
  6. mac OS X下PhpStorm+MAMP PRO+Xdebug+FireFox集成开发和断点调试环境配置
  7. Django Tutorial 学习笔记
  8. SQL Server 2000 “用户XX已经存在” 处理方法
  9. DOM基础总结
  10. Django文档——Model字段类型(Field Types)