Reputation: 1
I want to save the following struct into a binary file:
struct Usuario{
char nombre[256];
char apellido[256];
char ingresos[256];
std::vector<Bill> manejador_facturas;
};
Bill is also a struct:
struct Bill{
float monto;
int dia;
int mes;
int anio;
char empresa[256];
};
What I tried to write the information into the binary file was:
void Perfil::GuardarEnBinario(std::ostream &archivo) {
Usuario reg;
Bill auxiliar;
strcpy(reg.nombre, nombre.c_str());
strcpy(reg.apellido, apellido.c_str());
strcpy(reg.ingresos, ingresos.c_str());
for(size_t i = 0; i < manejador_facturas.size(); i++){
strcpy(auxiliar.empresa, manejador_facturas[i].empresa);
auxiliar.monto = manejador_facturas[i].monto;
auxiliar.dia = manejador_facturas[i].dia;
auxiliar.mes = manejador_facturas[i].mes;
auxiliar.anio = manejador_facturas[i].anio;
reg.manejador_facturas.push_back(auxiliar);
}
archivo.write((char*)®, sizeof(reg));
}
At first I thought it worked because the .exe worked fine, but the problem is that the information that I saved, wasn't showing at all when I tried to see it (don't know if the .bin was corrupted or what). The code for reading the struct is:
void Perfil::LeerDesdeBinario(std::istream &archivo) {
Usuario reg;
Bill auxiliar;
archivo.read((char*)®, sizeof(reg));
nombre = reg.nombre;
apellido = reg.apellido;
ingresos = reg.ingresos;
for(size_t i = 0; i < reg.manejador_facturas.size(); i++){
strcpy(auxiliar.empresa, reg.manejador_facturas[i].empresa);
auxiliar.monto = reg.manejador_facturas[i].monto;
auxiliar.dia = reg.manejador_facturas[i].dia;
auxiliar.mes = reg.manejador_facturas[i].mes;
auxiliar.anio = reg.manejador_facturas[i].anio;
manejador_facturas.push_back(auxiliar);
}
}
Upvotes: 0
Views: 136
Reputation: 99084
Welcome to Stack Overflow. Writing and reading complex structures is simply not that easy. (And you ought to have attempted simpler types before advancing to this one.)
Let us start with a char[256]
:
void GuardarEnBinario(char *A, std::ostream &archivo)
{
ofstream fout("data", ios::out | ios::binary);
archivo.write(A, 256);
}
void LeerDesdeBinario(char *B, std::istream &archivo)
{
archivo.read(B, 256);
}
Test this before you proceed.
Now try a Bill
:
void GuardarEnBinario(Bill &B, std::ostream &archivo)
{
archivo.write((char*)&B.monto, sizeof(float));
archivo.write((char*)&B.dia, sizeof(int));
GuardarEnBinario(B.empresa, archivo);
}
void LeerDesdeBinario(Bill &B, std::istream &archivo)
{
archivo.read((char*)&B.monto, sizeof(float));
archivo.read((char*)&B.dia, sizeof(int));
LeerDesdeBinario(B.empresa, archivo);
}
(I have omitted some of the fields; it should be clear how to handle them.)
Test this, and be sure that you understand it, before you proceed.
Now try a vector<Bill>
:
void GuardarEnBinario(std::vector<Bill> &V, std::ostream &archivo)
{
unsigned int n = V.size();
cout << n << endl;
archivo.write((char *) &n, sizeof(unsigned int));
for(unsigned int k=0; k<n; ++k)
{
GuardarEnBinario(V[k], archivo);
}
}
void LeerDesdeBinario(std::vector<Bill> &V, std::istream &archivo)
{
unsigned int n;
archivo.read((char *) &n, sizeof(unsigned int));
cout << n << endl;
Bill B;
for(unsigned int k=0; k<n; ++k)
{
LeerDesdeBinario(B, archivo);
V.push_back(B);
}
}
Once you have this working, the rest of your task should be easy.
Upvotes: 1