#include <stdio.h>
#include <stdlib.h>
#define MAX 50
//khai bao struct
typedef struct{
char name[30];
int diem;
}Student;
int input();
FILE *f;
Student *student;
int main()
{
int size = input();
return 0;
}
//doc du lieu trong file vao struct va in ra man hinh
int input()
{
if((f = fopen("bangdiem.txt","r")) == NULL){
printf("cannot open \n");
return;
}
int size = 0;
while(!feof(f)){
student = (Student *)malloc(sizeof(Student));
fscanf(f,"%s%d",student->name,&student->diem);
printf("%s %d\n",student->name,student->diem);
size++;
free(student);
}
printf("size = %d\n",size);
fclose(f);
return size;
}
nội dung file
ledinhduy 5
levanhoang 6
tranhuuthinh 8
kết qủa in ra
ledinhhuy 5
levanthanh 6
levantan 8
8
size = 4
tại sao khi dùng feof thì nó lại bị thừa ra số 8 ở kết qủa và gía trị size trả lại lại là 4?? Mọi người sửa lỗi giúp mình và nếu có cách nào hay hơn thì chỉ cho mình với.