I am trying to record a series of data in a struct in c ++. However, I have the problem that the field "activepolicy" (what should have only V and F) does not take the values I want: Saves those from the subsequent field. I think it's a problem related to how to capture Char values. But I was looking for cplusplus.com and I could not find anything that made me understand.
This is the struct:
struct poliza{
int nroPoliza;
char dniAsegurado[11];
char nombreAsegurado[50];
char apellidoAsegurado[50];
char cuotaAlDia[1];
char patenteAuto[10];
char polizaActiva[1];
int cantIncidentes;
};
With this method I charge it
bool cargarNuevaPoliza(void){
FILE *f;
poliza p;
if(f=fopen(ARCHIVOASEGURADOS, "a"))
{
cout << "Ingrese Numero de Poliza:" << endl;
cin >> p.nroPoliza;
cout << "Ingrese DNI:" << endl;
cin >> p.dniAsegurado;
cout << "Ingrese Nombre:" << endl;
cin >> p.nombreAsegurado;
cout << "Ingrese Apellido:" << endl;
cin >> p.apellidoAsegurado;
cout << "Posee la cuota al dia (V/F):" << endl;
cin >> p.cuotaAlDia;
cout << "Ingrese patente del auto:" << endl;
cin >> p.patenteAuto;
cout << "Poliza activa (V/F):" << endl;
cin >> p.polizaActiva;
cout << "Ingrese cantidad de Incidentes:" << endl;
cin >> p.cantIncidentes;
fwrite(&p, sizeof(poliza),1, f);
fclose(f);
return true;
}
return false;
}
With this I read:
void levantarAsegurados()
{
FILE*F;
int i = 0;
poliza V [1000];
F = fopen(ARCHIVOASEGURADOS,"rb");
fseek(F,0,SEEK_SET);
fread(&V[i],sizeof(poliza),1,F);
while (!feof(F))
{
cout << V[i].nroPoliza << endl;
cout << V[i].dniAsegurado << endl;
cout << V[i].nombreAsegurado << endl;
cout << V[i].apellidoAsegurado << endl;
cout << V[i].cuotaAlDia << endl;
cout << V[i].patenteAuto << endl;
cout << V[i].polizaActiva << endl;
cout << V[i].cantIncidentes << endl;
i++;
fread(&V[i],sizeof(poliza),1,F);
}
fclose(F);
}
The result I get is the following: