This commit is contained in:
hairrrrr
2020-02-12 14:50:41 +08:00
parent cf5994896a
commit b96ea9acfe
2 changed files with 46 additions and 1 deletions

View File

@@ -0,0 +1,45 @@
#include<stdio.h>
#include"student.h"
void read(FILE* fp, int index);
int main(void) {
FILE* fp = fopen("student.data", "r");
if (fp) {
fseek(fp, 0L, SEEK_END);//将文件读/写开始的位置定到结尾
long size = ftell(fp);//获得从文件开始到文件结尾的字节数
int index = 0;
int number = size / sizeof(Student);
printf("有 %d 个学生数据,你要看第几个\n", number);
scanf("%d", &index);
read(fp, index - 1);
}
else
printf("文件打开失败!\n");
}
void read(FILE* fp, int index) {
fseek(fp, index * sizeof(Student), SEEK_SET);
Student stu;
if (fread(&stu, sizeof(Student), 1, fp) == 1) {
printf("第 %d 个学生:\n", index + 1);
printf("\t姓名:%s\n", stu.name);
printf("\t性别:");
switch (stu.gender) {
case 0:
printf("\n");
break;
case 1:
printf("\n");
break;
default:
printf("其他\n");
break;
}
printf("\t年龄:%d\n", stu.age);
}
}

View File

@@ -52,7 +52,7 @@ void getList(Student* Stu, int num) {
int save(Student* Stu, int num) {
int ret = -1;
FILE* fp = fopen("student.data", "wx");
FILE* fp = fopen("student.data", "wx");//如果要向student.data反复写入数据将 wx 改为 x
if (fp) {
ret = fwrite(Stu, sizeof(Student), num, fp);
//记录 fwrite 的返回值:写入文件的学生个数