I am trying to write a series of data in a struct in c++. However, I have the problem that the "polizaActiva" field (which should have only V and F) does not take the values I want: It saves those of the subsequent field. I think it is a problem related to how to fetch Char values. But I was searching on cplusplus.com and couldn't 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 load 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 them:
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:
To put this:
You better leave it like this:
Simpler, clearer and easier to understand. Think of text strings as needing at least two characters (one to store a character and one to end the string).
More than anything because then you do:
And supposedly,
cin
it ends text strings... How is this supposed to end? I'm telling you... stepping on memory that doesn't belong to the array. However if you leave the variable as a simplechar
,cin
it will not treat the variable as a string and will not attempt to terminate it.Because of course, then you do:
And
cout
, since it finds an array of characters, it starts to extract values until it finds a null character... which is not going to be where you expect... the string terminator that you putcin
is no longer there because, presumably, it has been stepped on by later operations.If the latter does not work for you, let's see a possible example of use:
Initial memory (nothing is initialized):
operation:
memory status:
operation:
memory state (hypothetical example because it also depends on whether the machine is big-endian or little-endian... among other things:
If you now do:
The function will start looping through bytes and printing them until a '\0' is found... and as you can see the string terminator you had initially is lost.
The structure should look like this:
Although taking into account that you are in C++, it would be best to leave it like this:
Replaces char DailyQuota[1]; by char quotaAlDia; and char ActivePolicy[1]; by char policyActive; . That is, it removes the array declaration. Doesn't it give you an error in F = fopen(FILESECURED,"rb"); ?, it should because SECUREFILES is not in quotes