#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. Java集合源码分析

    Java集合工具包位于Java.util包下,包含了很多常用的数据结构,如数组.链表.栈.队列.集合.哈希表等.学习Java集合框架下大致可以分为如下五个部分:List列表.Set集合.Map映射.迭 ...

  2. shell命令行快速编辑命令

    ctrl r:命令行出现 reverse-i-search,输入字符将在输入历史中匹配命令 ctrl p:向前翻看历史 ctrl n:向后翻看历史 ctrl a:命令行首 ctrl e:命令行尾 ct ...

  3. C/C++输入输出总结

    *string类:  1.cin>>string时,遇到'\n'或者空格即停止,并且'\n'或空格仍留在输入里,即只读了一个单词或什么都没读,但string类自己处理好了空字符什么的.下一 ...

  4. ubuntu后台配置无线网络

    一.静态配置: 1.编辑 /etc/network/interfaces: auto loiface lo inet loopback auto wlan0iface wlan0 inet stati ...

  5. 带搜索的下拉框Chosen

    一:参考 https://harvesthq.github.io/chosen/ Chosen是一个jQuery插件 二:引入js文件 <link href="plug-in/chos ...

  6. struts2中的常量

    struts2中的常量: 在:struts2-core-2.1.8.1\org\apache\struts2\default.properties 文件里 <!-- 配制i18n国际化--> ...

  7. Xcode更改配色方案

    更改配色方案:Xcode > PReferences > Fonts & Color /********************************************** ...

  8. MFC VC6++学习笔记

    一.mfc中基于对话框程序添加菜单栏 1打开对话框资源,然后右键->属性->常规 里面有个"菜单" 下拉框,然后选择IDM_USER! 2打开对话框,右键属性,选择刚才 ...

  9. Cadence封装制作之表贴封装的制作

    以0805封装为例 1.打开PCB editor-> Allegro PCB Design XL 2.File -> New ① Drawing Type -> Package Sy ...

  10. proguardgui.bat来混淆已有的jar包

    1.U:\android-sdk\tools\proguard\bin\找到 proguardgui.bat,双击就可以弹出一个混淆的界面 2.加入不要混淆的,比如我们用的系统的,还有别人的jar 3 ...