|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct date
- {
- int day;
- int month;
- int year;
- };//结构体名的定义 自定义类型符
- typedef struct student
- {
- int num;
- char name[20];
- struct date birthday;
- }studnet;
- struct student *pstu;
- student stu;
- pstu = &stu;
- void FindByNum()
- {
- FILE *fp;
- int num;
- int find = 0;
- printf("按学号查找数据:");
- fp = fopen("C:\\Users\\yyp\\Desktop\\t4.txt","r+");
- if(fp == 0)
- {
- printf("Error!\n");
- exit(0);
- }
- else
- {
- printf("请输入学号:\n");
- scanf("%d",&num);
- }
- while(!feof(fp))
- {
- fscanf(fp,"%d%s%d%d%d\n",&pstu->num,pstu->name,&pstu->birthday.year,&pstu->birthday.month,&pstu->birthday.day);
- if(num == pstu->num)
- {
- printf("学号:%d\n",pstu->num);
- printf("名字:%s\n",pstu->name);
- printf("生日:%d-%d-%d\n",pstu->birthday.year,pstu->birthday.month,pstu->birthday.day);
- printf("\n");
- find = 1;
- }
- }
- if(!find)
- {
- printf("查无此人!\n");
- }
- fclose(fp);
- }
复制代码
代码如上,不明白的地方就是输入了一个学号之后,因为有个!feof(fp),所以fscanf()会持续执行到文件最后,也就是把所有的数据存储到结构体之中,
那找的话应该是怎么操作?下面再判断if ,加入输入的学号存在相等,那么输出的怎么保证是对应这个学号的信息呢。这里是书上的一个学生信息系统,有点懵 |
|