用结构体变量进行定义学生信息保存到文件后,如何求出里面共有多少个学生的信息
3条回答 默认 最新
qzjhjxj 2022-07-04 22:06关注供参考:
#include<stdio.h> struct Student { int num; char name[20]; char sex; int age; }tmp; int count_file() { FILE* fp; int cnt = 0; fp = fopen("student.txt","r"); if (fp == NULL) return cnt; while (1) { if (fread(&tmp,sizeof(struct Student),1,fp) != 1) break; cnt++; } fclose(fp); return cnt; } int main() { int n = -1; n = count_file(); printf("%d\n",n); return 0; }本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报