使用C++上的迭代读取和写入二进制文件
Reading and writing in binary file using iteration on 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<const char*> (&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<const char*> (&objPersonagens[i]) sizeof(PersonagemDesenho))
i++;
}
}
从您对std::getline()
的使用中可以清楚地看出,nomePersonagem
和nomeCriador
是std::string
值。 std::string
是一种动态类型(它包含指向存储在内存中其他位置的数据的指针(。当结构中包含动态数据时,不能按原样写入/读取结构。您将写入/读取指针本身,而不是指向的数据。
此问题的解决方案需要在写入时将数据序列化为更平坦的格式,并在读取时反序列化数据。
尝试更多类似的东西:
void writeSizeT(ostream &out, size_t value) {
out.write(reinterpret_cast<char*>(&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<char*>(&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();
}
相关文章:
- 如何在 c++ 中将二进制文件读取为字符串
- 从二进制文件读取到数组:前面的任意数字
- 提升二进制文件读取错误不支持的版本
- 无法将二进制文件读取到 std::vector<std::byte> 中C++
- 提升序列化在从二进制文件读取时引发异常
- 从二进制文件 C++ 读取的字符串数组写入
- 将二进制文件读取到 std::vector<bool>
- 将fortran生成的二进制文件读取到C++中的带符号整数数组中
- 将从二进制文件读取的字符串转换为整数
- 从/到C 的二进制文件读取和写作整数
- 通过将结构信息从二进制文件读取到数组中,然后返回到文件C
- 将 int 的二进制文件读取到字符串 c++
- 将结构从二进制文件读取到链表,导致无限循环
- 将二进制文件读取到向量元素的地址中
- c++从二进制文件读取/写入类
- Python:将二进制文件读取到一个结构中,然后拆包
- 从二进制文件读取/写入十六进制字节
- 简单的 C++ 二进制文件读取
- 从二进制文件 c++ 读取 16 位整数
- 如何优化c++二进制文件读取