I've a problem with the fwrite
function (I checked it using debugging on C::B).
This is the code:
struct studente
{ char Cognome_Nome[30];
char Matricola [11];
short Superati;
float Media_Pesata;
short Crediti;
};
typedef struct studente STUDENTE;
void main()
{ FILE *fp;
STUDENTE Dati;
if((fp = fopen("studente.dat","w+b")) == NULL)
printf("Error\n");
else
{ fflush(stdin);
printf("Inserire il cognome e nome: ");
fgets(Dati.Cognome_Nome, 30, stdin);
fflush(stdin);
printf("\nInserire la matricola: ");
fgets(Dati.Matricola, 11, stdin);
fflush(stdin);
printf("Inserire il numero di esami superati: ");
scanf("%hd", &Dati.Superati);
fflush(stdin);
printf("Inserire la media pesata: ");
scanf("%f", &Dati.Media_Pesata);
fflush(stdin);
printf("Inserire il numero di crediti: ");
scanf("%hd", &Dati.Crediti);
fwrite(&Dati, sizeof(STUDENTE), 1, fp);
}
}
I receive an Segmentation Fault when fwrite
is called. I can't understand where is the problem. I checked the fwrite
prototype and I think it's all right.
Thank you in advance.