For some reason, I can't read a file that contains "string"s with C-style. If I use an array of characters, then I can do it. But I want to do strings and I would like to know how to do it. When I print the b."x attribute" it shows random characters. And yes, I know I should be using c++ files. But this is purely for an educational purpose.
Code:
struct Boleta
{
string name;
string surename;
string legajo //156.455-6;
int cod_materia;
string date // 2022/10/26;
};
int main()
{
Boleta boleta;
FILE * f = fopen("DIAFINALES.DAT", "wb");
if(!f)
{
cout<<"Error al abrir el archivo 'DIAFINALES.DAT'"<<endl;
return 1;
}
while(true)
{
cout<<"Name: ", cin>>boleta.name;
cout<<"Surname: ", cin>>boleta.surename;
if(boleta.name == "NULO" && boleta.surename == "NULO")
break;
cout<<"Legajo: ", cin>>boleta.legajo;
cout<<"Exam date: ",cin>>boleta.date;
fwrite(&boleta, sizeof(boleta), 1, f);
}
fclose(f);
FILE * f1 = fopen("DIAFINALES.DAT", "rb");
if(!f1)
{
cout<<"Error al abrir el archivo 'DIAFINALES.DAT'"<<endl;
return 1;
}
Boleta b;
while(fread(&b, sizeof(b),1,f1))
{
cout<<"************************"<<b.legajo<<"******************************"<<endl;
cout<<"EXAM DATE: "<<b.date<<endl;
cout<<"Name and surname: "<<b.name<<" "<<b.surename<<endl;
cout<<"Code of subject: "<<b.cod_materia<<endl;
}
fclose(f1);
return 0;
}