2
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 it in a binary file.
I implemented the structure as follows:
typedef struct
{
char nome[50];
int idade;
int matricula;
} aluno;
In the main function, I did as follows:
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 was compiled without any mistakes, but when I type the student information, I can only read the information of 3 students, and not 5 students as the exercise asks. I wonder why this is happening and would also like to know if this method of storing in a binary file is correct.
Thanks for the reply Lacobus! But I have a doubt. You used size_t. What is the purpose of it?
– Renan