In the program, I need to implement a structure that represents a student (name, age, and enrollment). Using this structure, I have to write a program that reads the data of 5 students and stores them in a binary file.
Implemented the structure as follows:
typedef struct
{
char nome[50];
int idade;
int matricula;
} aluno;
In the main function, I did this:
int main(int args, char *arg[])
{
FILE *arq;
arq = fopen("registro.txt", "wb");
if(arq == NULL)
{
printf("Erro ao criar o arquivo");
exit(1);
}
aluno registro[5];
int i;
for(i = 0; i < 5; i++)
{
gets(registro[i].nome);
scanf("%d %d", ®istro[i].idade, ®istro[i].matricula);
fprintf(arq, "%s | %d | %d\n", registro[i].nome, registro[i].idade,
registro[i].matricula);
}
fclose(arq);
return 0;
}
The program has been compiled without any error, but when I enter the information of the student, I can only read the information of 3 students, not 5 students as the exercise asks. I would like to know why this is happening and I would also like to know if this method of storing in a binary file is correct.