码迷,mamicode.com
首页 > 编程语言 > 详细

c++二进制文件的读写

时间:2016-01-12 11:17:45      阅读:158      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!