c文件改成迭代服务器怎么改,使用C ++迭代在二进制文件中读写(示例代码)

我正在做一个涉及系统的功课,该系统将结构保存在二进制文件上,这是必要的(?)迭代的使用。

我写作时遇到问题(不确定是否正确):

void InserirDados () {

//var created to acknowledge the quantity of items that will be described

int quantidade;

cout << "Quantos personagens voce pretende inserir nesta sessao?" << endl;

cin >> quantidade;

//allocating memory like it's asked by the teacher

PersonagemDesenho* objPersonagem = new PersonagemDesenho[quantidade];

//declaring the flow of data

ofstream arquivo ("personagens.dat", ios::binary);

//flush buffer

cin.ignore ();

cout << "(A utilizacao de espacos e' permitida para todos os itens a seguir)" << endl;

//describing items

for (int i = 0; i < quantidade; i++) {

cout << " - PERSONAGEM NUMERO: " << i + 1 << endl;

cout << "Digite o nome do personagem a ser inserido" << endl;

//getline for getting more than 1 word

getline(cin, objPersonagem[i].nomePersonagem);

cout << "Digite o nome do criador do personagem" << endl;

getline(cin, objPersonagem[i].nomeCriador);

//writing code

arquivo.write(reinterpret_cast (&objPersonagem[i]), sizeof(PersonagemDesenho));

}

cout << "As informacoes serao salvas no arquivo "personagens.dat"" << endl;

//closing file

arquivo.close();

}

和阅读数据:

void ListaDados () {

ifstream arquivo ("personagens.dat", ios::binary);

int i = 0;

while (???) {

arquivo.read(reinterpret_cast (&objPersonagens[i]) sizeof(PersonagemDesenho))

i++;

}

}

答案

从你使用std::getline()可以清楚地看出,nomePersonagem和nomeCriador是std::string值。 std::string是一种动态类型(它包含指向存储在别处的数据的指针)。当它包含动态数据时,您不能按原样编写/读取结构。您可以编写/读取指针本身,而不是指向的数据。

该问题的解决方案要求在写入时将数据序列化为更平坦的格式,并在读取时对数据进行反序列化。

尝试更像这样的东西:

void writeSizeT(ostream &out, size_t value) {

out.write(reinterpret_cast(&value), sizeof(value));

}

void writeString(ostream &out, const string &value) {

size_t len = value.size();

writeSizeT(out, len);

out.write(s.c_str(), len);

}

void InserirDados () {

//var created to acknowledge the quantity of items that will be described

int quantidade;

cout << "Quantos personagens voce pretende inserir nesta sessao?" << endl;

cin >> quantidade;

//allocating memory like it's asked by the teacher

PersonagemDesenho* objPersonagem = new PersonagemDesenho[quantidade];

//declaring the flow of data

ofstream arquivo ("personagens.dat", ios::binary);

writeSizeT(arquivo, quantidade);

//flush buffer

cin.ignore ();

cout << "(A utilizacao de espacos e' permitida para todos os itens a seguir)" << endl;

//describing items

for (int i = 0; i < quantidade; i++) {

cout << " - PERSONAGEM NUMERO: " << i + 1 << endl;

//getline for getting more than 1 word

cout << "Digite o nome do personagem a ser inserido" << endl;

getline(cin, objPersonagem[i].nomePersonagem);

cout << "Digite o nome do criador do personagem" << endl;

getline(cin, objPersonagem[i].nomeCriador);

//writing code

writeString(arquivo, objPersonagem[i].nomePersonagem);

writeString(arquivo, objPersonagem[i].nomeCriador);

}

cout << "As informacoes serao salvas no arquivo "personagens.dat"" << endl;

//closing file

arquivo.close();

// freeing memory

delete[] objPersonagem;

}

size_t readSizeT(istream &in) {

size_t value;

in.read(reinterpret_cast(&value), sizeof(value));

return value;

}

string readString(istream &in) {

string value;

size_t len = readSizeT(in);

if (len > 0) {

value.resize(len);

in.read(&s[0], len);

}

return value;

}

void ListaDados () {

ifstream arquivo ("personagens.dat", ios::binary);

size_t quantidade = readSizeT(arquivo);

//allocating memory like it's asked by the teacher

PersonagemDesenho* objPersonagem = new PersonagemDesenho[quantidade];

for(size_t i = 0; i < quantidade; ++i) {

//reading code

objPersonagem[i].nomePersonagem = readString(arquivo);

objPersonagem[i].nomeCriador = readString(arquivo);

}

// freeing memory

delete[] objPersonagem;

//closing file

arquivo.close();

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值