Мне нужно вывести из файла всю структуру по ключевому слову ID,то бишь мне надо ввести несколько структур(ID,Фамилия,сред.балл) и вывести какую-либо структуру по ключевому слову ID,но я столкнулся с ошибкой(Просто нет никакого вывода).Я в ступоре,и не понимаю как её можно исправить.Прошу указать способ устранения этой проблемы.
#include <stdio.h>
#include <windows.h>
void main() {
struct student {
int id_student;
char name_student[30];
float average_student;
} e; // structure Employee e;
FILE *f;
if ((f = fopen("f2.dat", "wb")) == NULL) {
printf("\nCannot open file for writing");
exit(1);
}
int n, i;
//write to file
printf("\n N-?");
scanf("%d", &n);
for (i = 0; i < n; i++) {
//structure е
printf("\ID:");
scanf("%d", &e.id_student);
printf("\nName:");
scanf("%s", &e.name_student);
printf("\nAverage mark stats:");
scanf("%f", &e.average_student);
//writing structure to file
fwrite(&e, sizeof(e), 1, f);
}
fclose(f);
//read from file
if ((f = fopen("f2.dat", "rb")) == NULL) {
printf("\nCannot open file for reading");
exit(1);
}
int buffer;
printf("Input ID");
scanf("%d", &buffer);
// output to screen
while (fread(&e, sizeof(e), 1, f)) {
if (e.id_student == buffer) {
printf("\n%d %s %f", e.id_student, e.name_student, e.average_student);
}
fclose(f);
}
}
Мой ввод
N-?2
ID:4
Name:Dima
Average mark stats:5.0
ID:2
Name:Sasha
Average mark stats:6.0
Input ID 2
Process finished with exit code 0