java向量的文件读写,C将二进制文件读入自定义类的向量

我试图将一些数据转储到二进制文件中,然后从其他地方加载它,但在尝试读取保存的文件时,我一直收到Access Violation读取错误或_Block_Type_Is_Valid(pHead-> nBlockUse)错误 .

这是保存文件的代码:

ofstream ofs(filename, ios_base::out | ios_base::binary);

// Write how many pop this file includes.

ofs.write((char*)&pop_size, sizeof(int));

// And the vectors

ofs.write((char*)&tmp_p[0], (tmp_p.size() * sizeof(TSOP)) );

ofs.close();

这是阅读方法(我尝试了几种组合):

ifstream ifs;

ifs.open(filename, ios_base::in | ios_base::binary);

int pop_size = -1;

ifs.read((char*)&pop_size, sizeof(int));

ifs.read(reinterpret_cast(&tmp_p[0]), sizeof(TSOP) * tmp_p.size());

ifs.close();

(我也尝试保存,并在循环中读取每个元素,而不是reinterpret_cast,我只是使用(char *),没有任何工作..)

向量包含类TSOP的元素,如下所示:

class TSOP

{

public:

TSOP();

virtual ~TSOP();

void show();

TIndividual indiv;

vector indiv_M;

vector namda;

vector namda_t;

vector table; // the vector for the indexes of neighboring subproblems

bool updated;

void operator=(const TSOP&sub2);

};

和个人:

class TIndividual{

public:

TIndividual();

virtual ~TIndividual();

vector x_var;

//boost::dynamic_bitset<> x_var_bits;

//std::bitset<> x_var_bits2;

vector y_obj;

vector y_obj_n;

vector y_blc;

//for constraints

vector y_bio;

double y_bio_sum;

bool feasible;

//for constraints

//for NSGA-III

int pi_idx;

double d;

bool dominated;

//short dominateds;

void rnd_init();

void obj_eval(int count_index_start = 0);

void calc_balance();

bool operator

bool operator>(const TIndividual &ind2);

bool operator==(const TIndividual &ind2);

void operator=(const TIndividual &ind2);

void show_objective();

void show_variable();

long id;

double var;

};

我读到保存二进制文件时指针和字符串可能有问题,但这些类中没有任何指针或字符串 . 它主要由向量,整数和双精度组成 .

通过使用cout,我能够读取pop_size的第一个int,然后在尝试读取保存的向量的第一个元素时,它崩溃了 .

我在这做错了什么?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值