#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

Qt: 读写二进制文件(写对象, 原始数据等)的更多相关文章

  1. Qt读写二进制文件

    http://blog.csdn.net/mjlsuccess/article/details/22194653 http://www.cnblogs.com/weiweiqiao99/archive ...

  2. C/C++读写二进制文件

    C++读写二进制文件 最近在给android层提供支持,因此代码都是用标准库库函数写出来的,好多windows和第三方的库不能或者很难使用,下面有我在读写二进制文件时候的一些心得,也算是一种总结吧 1 ...

  3. C++入门到理解之文件操作(文本文件的读写+二进制文件的读写)

    原文地址http://www.javayihao.top/detail/168 一:概述 1.程序在运行中产生的数据都是临时数据,程序一旦运行结束会被释放,可以通过文件相关的操作将数据持久保存. 2. ...

  4. 【转】C++读写二进制文件

    原文网址:http://blog.csdn.net/lightlater/article/details/6364931 摘要: 使用C++读写二进制文件,在开发中操作的比较频繁,今天有幸找到一篇文章 ...

  5. [Matlab+C/C++] 读写二进制文件

    introduction 因为Matlab操作简单.方便,它被应用于很多领域:音频处理,图像处理,数值计算等.尽管MATLAB容易操作,但受限于他的语言解释机制,MATLAB的执行速度通常较低.C/C ...

  6. JavaEE JDBC 读写LOB大对象

    JDBC 读写LOB大对象 @author ixenos LOB 除了数字.字符串和日期之外,许多数据库还可以存储大对象,例如图片或其他数据, 在SQL中,二进制(字节型)大对象称为BLOB,字符型大 ...

  7. QT 读写sqllite数据库

    QT 读写sqllite数据库 分类: 技术资料2014-04-10 10:39 84人阅读 评论(0) 收藏 举报 #include <QtGui/QApplication> #incl ...

  8. Qt打开外部程序和文件夹需要注意的细节(Qt调用VC写的动态库,VC需要用C的方式输出函数,否则MinGW32编译过程会报错)

    下午写程序中遇到几个小细节,需要在这里记录一下. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 QProcess *process = new QProcess(this ...

  9. c#写对象来读取TXT文本文件

    本博文让你知道怎样写对象,怎样读取文本文件,怎样根据实际条件判断与获取需要的文本行.参考下面网友的问题,根据源文来看,有些行输出的格式,需要把“,”替换为空格. 第一行还附加入后面的子行每一行的后面, ...

随机推荐

  1. jqGrid API 相关

    取消所有选中的行: $("jqgridtableid").trigger("reloadGrid"): 设定选中行,可设定多行选中: $("jqgri ...

  2. POJ 3928 Ping pong

    题目链接:http://poj.org/problem?id=3928 乒乓比赛,有N个人参加,输入每个玩家的技能等级,对每个人设置一个特定ID和一个技能值,一场比赛需要两个选手和一个裁判,只有当裁判 ...

  3. spring-cloud-turbine

    turbine主要用于聚合hystrix的监控数据 依赖pom <dependencyManagement> <dependencies> <dependency> ...

  4. CSS制作图片水平垂直居中 亲测推荐

    空白标签实现图片的垂直居中 这种方法很有意思,也很有独特之处,我的思路也是来自于张鑫旭-鑫空间-鑫生活写的<大小不固定的图片.多行文字的水平垂直居中>一文中的使用空白图片实现垂直对齐.他主 ...

  5. 1020. Tree Traversals (序列建树)

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  6. HDFS知识体系 思维导图

  7. Oracle的Export用法

    Oracle 的 Export 命令用于导出数据库信息,既可以导出表结构,也可以导出数据,表空间,或者按用户导出等等.按照通常的说法,该命令主要是用于数据库的迁移或者备份的.下面就介绍一下该命令的部分 ...

  8. 云主机上搭建squid3代理服务器

    目录 目录 具体流程 修改配置文件 问题 维基整理 代理服务器 Squid (软件) SOCKS SOCKS代理 参考:http://raysmond.com/node/79 具体流程 在服务器上安装 ...

  9. Many To one 多对一

    一.创建实体类:多方存一方的对象.set/get 二.编写对象的xml文件 别忘记在confg.xml映射! 三.编写接口 四.方法测试

  10. phpd读取txt文件(自动解析换行)

    <form id="form" method="post" action="whois.php"> <?php $newf ...