关于读取文本文件的问题,无法查询
程序代码:
#include "stdio.h" #include "string.h" #include "stdlib.h" #include "conio.h" #define LEN 1024 void search_information(char search_in[LEN]); void ccss_save(); void ccss_read(); void vprint(); void searchid(int search_id); void change_stu(char change_num[LEN]); void Interface(); void gn_choose(); struct ccss { char num[6]; char cname[20]; char csex[6]; int iage; int igrade; int iclass; int iseat; float fCha; float fMath; float fEng; double dtot; double dave; }stu[LEN]; int count_1; //录入学生成员信息 void create_stu() { count_1=0; char yn[4]; printf("选择y或者n进行操作:\n"); scanf("%s",yn); while((strcmp(yn,"y"))==0||(strcmp(yn,"Y"))==0) { printf("学号:"); scanf("%s",stu[count_1].num); printf("姓名:"); scanf("%s",stu[count_1].cname); printf("性别:"); scanf("%s",stu[count_1].csex); printf("年龄:"); scanf("%d",&stu[count_1].iage); printf("年级:"); scanf("%d",&stu[count_1].igrade); printf("班级:"); scanf("%d",&stu[count_1].iclass); printf("座位:"); scanf("%d",&stu[count_1].iseat); printf("三科成绩:\n"); printf("语文:"); scanf("%f",&stu[count_1].fCha); printf("数学:"); scanf("%f",&stu[count_1].fMath); printf("英语:"); scanf("%f",&stu[count_1].fEng); count_1++; getch(); printf("选择y或者n进行操作:\n"); scanf("%s",yn); } printf("录入完成!\n"); printf("按任意键返回........\n"); system("pause"); system("cls"); Interface(); } //显示学生录入的信息 void vprint() { int count_prit; printf("学号\t姓名\t 性别\t 年龄\t 年级\t 班级\t 座位\t 语文\t 数学\t 英语\t 总成绩\t 平均成绩\n"); printf("----------------------------------------------------------------------------------------------------------------------\n"); for(count_prit=0;count_prit<count_1;count_prit++) { stu[count_prit].dtot=stu[count_prit].fCha+stu[count_prit].fMath+stu[count_prit].fEng; stu[count_prit].dave=stu[count_prit].dtot/3; printf("%s\t %s\t %s\t %d\t %d\t %d\t %d\t %.2f\t %.2f\t %.2f\t %.2lf\t %.2lf\n", stu[count_prit].num, stu[count_prit].cname, stu[count_prit].csex, stu[count_prit].iage, stu[count_prit].igrade, stu[count_prit].iclass, stu[count_prit].iseat, stu[count_prit].fCha, stu[count_prit].fMath, stu[count_prit].fEng, stu[count_prit].dtot, stu[count_prit].dave ); } printf("按任意键返回........\n"); system("pause"); system("cls"); Interface(); } //查询是否有该学号的信息 void searchid(char search_id[LEN]) { int count_search; for(count_search=0;count_search<count_1;count_search++) { if((strcmp(stu[count_search].num,search_id))==0) printf("%d\n",count_search+1); else printf("没有该学号的信息!\n"); } printf("按任意键返回........\n"); system("pause"); system("cls"); Interface(); } //查询学生信息 void search_information(char search_in[LEN]) { int search_infor; for(search_infor=0;search_infor<count_1;search_infor++) (问题:数据读取后,只能查询第一条信息,第一条信息之后查询不到!) { stu[search_infor].dtot=stu[search_infor].fCha+stu[search_infor].fMath+stu[search_infor].fEng; stu[search_infor].dave=stu[search_infor].dtot/3; if((strncmp(stu[search_infor].num,search_in,2))==0) { printf("找到%d学号的信息!\n",search_infor+1); printf("学号\t姓名\t 性别\t 年龄\t 年级\t 班级\t 座位\t 语文\t 数学\t 英语\t 总成绩\t 平均成绩\n"); printf("----------------------------------------------------------------------------------------------------------------------\n"); printf("%s\t %s\t %s\t %d\t %d\t %d\t %d\t %.2f\t %.2f\t %.2f\t %.2lf\t %.2lf\n", stu[search_infor].num, stu[search_infor].cname, stu[search_infor].csex, stu[search_infor].iage, stu[search_infor].igrade, stu[search_infor].iclass, stu[search_infor].iseat, stu[search_infor].fCha, stu[search_infor].fMath, stu[search_infor].fEng, stu[search_infor].dtot, stu[search_infor].dave ); break; } else { printf("没有找到匹配的学生信息!\n"); exit(1); } } printf("按任意键返回........\n"); system("pause"); system("cls"); Interface(); } //修改指定学号的学生信息 void change_stu(char change_num[LEN]) { int count_change; char yn_change[4]; for(count_change=0;count_change<50;count_change++) { stu[count_change].dtot=stu[count_change].fCha+stu[count_change].fMath+stu[count_change].fEng; stu[count_change].dave=stu[count_change].dtot/3; if((strcmp(stu[count_change].num,change_num))==0) { printf("找到%d学号的信息!\n",count_change+1); printf("学号\t姓名\t 性别\t 年龄\t 年级\t 班级\t 座位\t 语文\t 数学\t 英语\t 总成绩\t 平均成绩\n"); printf("----------------------------------------------------------------------------------------------------------------------\n"); printf("%s\t %s\t %s\t %d\t %d\t %d\t %d\t %.2f\t %.2f\t %.2f\t %.2lf\t %.2lf\n", stu[count_change].num, stu[count_change].cname, stu[count_change].csex, stu[count_change].iage, stu[count_change].igrade, stu[count_change].iclass, stu[count_change].iseat, stu[count_change].fCha, stu[count_change].fMath, stu[count_change].fEng, stu[count_change].dtot, stu[count_change].dave ); } else { printf("没有找到匹配的学生信息!\n"); exit(1); } } printf("是否修改信息(y or n):\n"); scanf("%s",yn_change); if((strcmp(yn_change,"y"))==0||(strcmp(yn_change,"Y"))==0) { printf("学号:"); scanf("%s",stu[count_change].num); printf("姓名:"); scanf("%s",stu[count_change].cname); printf("性别:"); scanf("%s",stu[count_change].csex); printf("年龄:"); scanf("%d",&stu[count_change].iage); printf("年级:"); scanf("%d",&stu[count_change].igrade); printf("班级:"); scanf("%d",&stu[count_change].iclass); printf("座位:"); scanf("%d",&stu[count_change].iseat); printf("三科成绩:\n"); printf("语文:"); scanf("%f",&stu[count_change].fCha); printf("数学:"); scanf("%f",&stu[count_change].fMath); printf("英语:"); scanf("%f",&stu[count_change].fEng); getch(); } else exit(1); printf("修改成功!\n"); printf("按任意键返回........\n"); system("pause"); system("cls"); Interface(); } //保存数据 void ccss_save() { FILE* fp; int count_save=0; fp=fopen("D:\\data_stu.txt","a+"); if(fp==NULL) { printf("error!\n"); } while(count_save<count_1) { stu[count_save].dtot=stu[count_save].fCha+stu[count_save].fMath+stu[count_save].fEng; stu[count_save].dave=stu[count_save].dtot/3; fprintf(fp,"%s\t %s\t %s\t %d\t %d\t %d\t %d\t %.2f\t %.2f\t %.2f\t %.2lf\t %.2lf\r\n", stu[count_save].num, stu[count_save].cname, stu[count_save].csex, stu[count_save].iage, stu[count_save].igrade, stu[count_save].iclass, stu[count_save].iseat, stu[count_save].fCha, stu[count_save].fMath, stu[count_save].fEng, stu[count_save].dtot, stu[count_save].dave ); count_save++; } fclose(fp); printf("保存成功!\n"); printf("按任意键返回........\n"); system("pause"); system("cls"); Interface(); } //读取txt文件的数据 void ccss_read() { count_1=0; struct ccss read_stu; char filename[200]; FILE* fp_read; int count_read=0; printf("输入要载入的文件完整路径:"); scanf("%s",filename); fp_read=fopen(filename,"rt"); if(fp_read==NULL) { printf("error!\n"); } fseek(fp_read,0L,SEEK_SET); while(!feof(fp_read)) { //获得数据 fscanf(fp_read,"%s",read_stu.num); fscanf(fp_read,"%s",read_); fscanf(fp_read,"%s",read_stu.csex); fscanf(fp_read,"%d",&read_stu.iage); fscanf(fp_read,"%d",&read_stu.igrade); fscanf(fp_read,"%d",&read_stu.iclass); fscanf(fp_read,"%d",&read_stu.iseat); fscanf(fp_read,"%f",&read_stu.fCha); fscanf(fp_read,"%f",&read_stu.fMath); fscanf(fp_read,"%f",&read_stu.fEng); fscanf(fp_read,"%lf",&read_stu.dtot); fscanf(fp_read,"%lf\n",&read_stu.dave); //下面只是用来测试是否已经正确读取到数据 printf("%s\t",read_stu.num); printf("%s\t",read_); printf("%s\t",read_stu.csex); printf("%d\t",read_stu.iage); printf("%d\t",read_stu.igrade); printf("%d\t",read_stu.iclass); printf("%d\t",read_stu.iseat); printf("%.2f\t",read_stu.fCha); printf("%.2f\t",read_stu.fMath); printf("%.2f\t",read_stu.fEng); printf("%.2lf\t",read_stu.dtot); printf("%.2lf\n",read_stu.dave); stu[count_1]=read_stu;//全局结构变量stu得到读取的结构数据,即放入缓冲区 count_read++;//这个没用的,可以忽略。 count_1++; //计次递增 } fclose(fp_read); printf("按任意键返回........\n"); system("pause"); Interface(); } //删除指定学号 void delete_stu() { int delnum; int count_del; char delstu[6]; char cdel[6]; printf("输入要删除的学号:"); scanf("%s",delstu); for(delnum=0;delnum<50;delnum++) { if((strcmp(stu[delnum].num,delstu))==0) { printf("找到该学号的信息!\n"); continue; } } printf("是否删除:(y or n)"); scanf("%s",cdel); if((strcmp(cdel,"y"))==0||(strcmp(cdel,"Y"))==0) { for(count_del=delnum;count_del<count_1-1;count_del++) { stu[count_del]=stu[count_del+1]; } } else { printf("退出操作!\n"); exit(1); } printf("删除成功!\n"); printf("按任意键返回........\n"); system("pause"); system("cls"); Interface(); } //界面 void Interface() { printf("\n"); printf("\t\t\t===================学生管理系统==================\n"); printf("\t\t\t*\t\t\t\t\t");printf("\t*\n"); printf("\t\t\t*\t\t");printf(" 1.录入信息\t");printf("\t\t*\n"); printf("\t\t\t*\t\t\t\t\t");printf("\t*\n"); printf("\t\t\t*\t\t");printf(" 2.查询信息\t");printf("\t\t*\n"); printf("\t\t\t*\t\t\t\t\t");printf("\t*\n"); printf("\t\t\t*\t\t");printf(" 3.显示信息\t");printf("\t\t*\n"); printf("\t\t\t*\t\t\t\t\t");printf("\t*\n"); printf("\t\t\t*\t\t");printf(" 4.修改信息\t");printf("\t\t*\n"); printf("\t\t\t*\t\t\t\t\t");printf("\t*\n"); printf("\t\t\t*\t\t");printf(" 5.保存信息\t");printf("\t\t*\n"); printf("\t\t\t*\t\t\t\t\t");printf("\t*\n"); printf("\t\t\t*\t\t");printf(" 6.加载信息\t");printf("\t\t*\n"); printf("\t\t\t*\t\t\t\t\t");printf("\t*\n"); printf("\t\t\t*\t\t");printf(" 7.删除信息\t");printf("\t\t*\n"); printf("\t\t\t*\t\t\t\t\t");printf("\t*\n"); printf("\t\t\t*\t\t");printf(" 8.退出操作\t");printf("\t\t*\n"); printf("\t\t\t*\t\t\t\t\t");printf("\t*\n"); printf("\t\t\t===================学生管理系统==================\n"); gn_choose(); } void gn_choose() { char sear_num[6]; char chagne_number[6]; int choose_num; printf("输入需要操作的项目:\n"); scanf("%d",&choose_num); switch(choose_num) { case 1: create_stu(); break; case 2: printf("输入要查找的学号:"); scanf("%s",sear_num); search_information(sear_num); break; case 3: vprint(); break; case 4: printf("输入要修改的学号:"); scanf("%s",chagne_number); change_stu(chagne_number); break; case 5: ccss_save(); break; case 6: ccss_read(); break; case 7: delete_stu(); break; case 8: exit(0); break; default: printf("操作错误,请重新操作!\n"); break; } Interface(); } int main() { Interface(); /*char id_s[6]; create_stu(); vprint(); printf("输入要查询的学号:"); scanf("%s",id_s); searchid(id_s); //ccss_save(); //ccss_read(); //vprint();*/ return 0; }
我的问题是:通过完全路径读取到了txt文件的数据,并保存到stu,能正常打印出来,查是查询只能查询第一条数据。之后的数据完全查询不到。请问是因为fscanf读取的原因。还是其它原因?
调试图片无法上传。。点上传没反应。。呃。。
[此贴子已经被作者于2016-6-7 08:32编辑过]