首页 > 编程语言 > 详细

c++二进制文件的读写

时间:2016-01-12 11:16:19      阅读:229      评论:0      收藏:0      [点我收藏+]
#include "stdafx.h"

#include "string"
#include <fstream>

using namespace std;

class C
{
public:
    C():i(),str(){};//初始化,非赋值
    C(int iP,string strP):i(iP),str(strP){};
private:
    int i;
    string str;
};

void main()
{
    char *filePath = "D:/123.data";

    ofstream fout;
    fout.open(filePath,ofstream::binary);
    int n = 100;
    fout.write((char *)&n,sizeof(n));//读写都要做这样的转换(char *)&n
    C cOut(1,"ok");
    fout.write((char *)&cOut,sizeof(C));
    fout.close();

    ifstream fin;
    fin.open(filePath,ifstream::binary);
    int m = 0;
    fin.read((char *)&m,sizeof(int));
    C cIn;
    fin.read((char *)&cIn,sizeof(C));
    fin.close();
}

 

c++二进制文件的读写

原文:http://www.cnblogs.com/rednodel/p/5123405.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!