关于文件读写的问题
我用键盘输入的信息至屏幕,然后保存入文件中,但是我打开保存信息的文件时,里边都是乱码然而,我运行程序,从文件中读取信息时,却是正常的。也就是说直接打开文件看到的不是我要的结果,然而通过程序读文件时却是正常的。
想请问一下,怎么可以让直接打开文件时,里边的信息也是正常的?谢谢!
程序代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #define HEADER1 " -----------------------------STUDENT-------------------------\n" #define HEADER2 " |number |name |Comp|Math|Eng |sum |ave |mici|\n" #define HEADER3 " |----------|---------------|----|----|----|----|-------|----|\n" #define FORMAT " |%-10s|%-15s|%4d|%4d|%4d|%4d|%.2f |%4d|\n" #define DATA p->data.num,p->data.name,p->data.cgrade,p->data.mgrade,p->data.egrade,p->data.total,p->data.ave,p->data.mingci #define END " ------------------------------------------------------------\n" int saveflag=0; struct student { char num[10]; char name[15]; int cgrade; int mgrade; int egrade; int total; float ave; int mingci; }; typedef struct Node { struct student data; struct node *next; }node,*link; void menu() { system("cls"); textcolor(10); gotoxy(10,5); cprintf(" The Students' Grade Mangement System \n"); gotoxy(10,8); cprintf("*****************************Menu********************************\n"); gotoxy(10,9); cprintf(" * 1 input record 2 delete record *\n"); gotoxy(10,10); cprintf(" * 3 search record 4 modify record *\n"); gotoxy(10,11); cprintf(" * 5 insert record 6 count record *\n"); gotoxy(10,12); cprintf(" * 7 sort record 8 save record *\n"); gotoxy(10,13); cprintf(" * 9 display record 0 quit system *\n"); gotoxy(10,14); cprintf("*****************************************************************\n"); } void printheader() { printf(HEADER1); printf(HEADER2); printf(HEADER3); } void printdata(node *pp) { node* p; p=pp; printf(FORMAT,DATA); } void disp(link l) { node *p; p=l->next; if(!p) { printf("\n=====>not student record!\n"); getchar(); return; } printf("\n\n"); printheader(); while(p) { printdata(p); p=p->next; printf(HEADER3); } getchar(); } void wrong() { printf("\n\n\n\n\n*****************Error:input has wrong!press any key to continue*****************\n"); getchar(); } void nofind() { printf("\n=====>not find this student!\n"); } node* locate(link l,char findmess[],char nameornum[]) { node* r; if(strcmp(nameornum,"num")==0) { r=l->next; while(r) { if(strcmp(r->data.num,findmess)==0) return r; r=r->next; } } else if(strcmp(nameornum,"name")==0) { r=l->next; while(r) { if(strcmp(r->data.name,findmess)==0) return r; r=r->next; } } return 0; } void stringinput(char *t,int lens,char *notice) { char n[255]; do { printf(notice); scanf("%s",n); if(strlen(n)>lens) printf("\n exceed the required length!\n"); }while(strlen(n)>lens); strcpy(t,n); } int numberinput(char *notice) { int t=0; do{ printf(notice); scanf("%d",&t); if(t>100||t<0) printf("\n score must in [0,100]!\n"); }while(t>100||t<0); return t; } void add(link l) { node *p,*r,*s; char ch,flag=0,num[10]; r=l; s=l->next; system("cls"); disp(l); while(r->next!=NULL) r=r->next; while(1) { while(1) { stringinput(num,10,"input number(press '0' return menu):"); flag=0; if(strcmp(num,"0")==0) {return;} s=l->next; while(s) { if(strcmp(s->data.num,num)==0) { flag=1; break; } s=s->next; } if(flag==1) { getchar(); printf("=====>The number %s is not existing,try again?(y/n):",num); scanf("%c",&ch); if(ch=='y'||ch=='Y') continue; else return; } else {break;} } p=(node *)malloc(sizeof(node)); if(!p) { printf("\n allocate memory failure "); return ; } strcpy(p->data.num,num); stringinput(p->data.name,15,"Name:"); p->data.cgrade=numberinput("C language score[0-100]:"); p->data.mgrade=numberinput("Math score[0-100]:"); p->data.egrade=numberinput("English score[0-100]:"); p->data.total=p->data.egrade+p->data.mgrade+p->data.cgrade; p->data.ave=(float)(p->data.total/3); p->data.mingci=0; p->next=NULL; r->next=p; r=p; saveflag=1; } return; } void qur(link l) { int select; char searchinput[20]; node* p; if(!l->next) { system("cls"); printf("=====>nostudent record!\n"); getchar(); return; } system("cls"); printf("\n ======>1 search by number ======>2 search by name\n"); printf(" please choice[1,2]"); scanf("%d",&select); if(select==1) { stringinput(searchinput,10,"input the existing student number:"); p=locate(l,searchinput,"num"); if(p) { printheader(); printdata(p); printf(END); printf("press any key to return"); getchar(); } else nofind(); getchar(); } else if(select==2) { stringinput(searchinput,15,"input the existing student name:"); p=locate(l,searchinput,"name"); if(p) { printheader(); printdata(p); printf(END); printf("press any key to return"); getchar(); } else nofind(); getchar(); } else wrong(); getchar(); } void del(link l) { int sel; node *p,*r; char findmess[20]; if(!l->next) { system("cls"); printf("\n=====>no student record!\n"); getchar(); return; } system("cls"); disp(l); printf("\n =====>1 delete by number =====>2 delete by name\n"); printf(" please choice[1,2]"); scanf("%d",&sel); if(sel==1) { stringinput(findmess,10,"input the existing student number:"); p=locate(l,findmess,"num"); if(p) { r=l; while(r->next!=p) r=r->next; r->next=p->next; free(p); printf("\n=====>delete success!\n"); getchar(); saveflag=1; } else nofind(); getchar(); } else if(sel==2) { stringinput(findmess,10,"input the existing student name:"); p=locate(l,findmess,"name"); if(p) { r=l; while(r->next!=p) r=r->next; r->next=p->next; free(p); printf("\n=====>delete success!\n"); getchar(); saveflag=1; } else nofind(); getchar(); } else wrong(); getchar(); } void modify(link l) { node *p; char findmess[20]; if(!l->next) { system("cls"); printf("\n=====>no student record!\n"); getchar(); return; } system("cls"); printf("modify student recorder"); disp(l); stringinput(findmess,10,"input the existing student number:"); p=locate(l,findmess,"num"); if(p) { printf("Number:%s,\n",p->data.num); printf("Name:%s,",p->data.name); stringinput(p->data.name,15,"input new name:"); printf("C language score:%d,",p->data.cgrade); p->data.cgrade=numberinput("C language score[0-100]:"); printf("Math score:%d,",p->data.mgrade); p->data.mgrade=numberinput("Math score[0-100]:"); printf("English score:%d,",p->data.egrade); p->data.egrade=numberinput("English score[0-100]:"); p->data.total=p->data.egrade+p->data.cgrade+p->data.mgrade; p->data.ave=(float)(p->data.total/3); p->data.mingci=0; printf("\n=====>modify success!\n"); disp(l); saveflag=1; } else nofind(); getchar(); } void insert(link l) { link p,v,newinfo; char ch,num[10],s[10]; int flag=0; v=l->next; system("cls"); disp(l); while(1) { stringinput(s,10,"Please input insert location after the number:"); flag=0;v=l->next; while(v) { if(strcmp(v->data.num,num)==0) {flag=1;break;} v=v->next; } if(flag==1) break; else { getchar(); printf("\n=====>The number %s is not existing,try again?(y/n):",s); scanf("%c",&ch); if(ch=='y'||ch=='Y') continue; else return; } } stringinput(num,10,"input new student number:"); v=l->next; while(v) { if(strcmp(v->data.num,s)==0) { printf("=====>sorry,the new number:'%s' is existing !\n",num); printheader(); printdata(v); printf("\n"); getchar(); return; } v=v->next; } newinfo=(node*)malloc(sizeof(node)); if(!newinfo) { printf("\nallocate memory faolure "); return; } strcpy(newinfo->data.num,num); stringinput(newinfo->data.name,15,"Name:"); newinfo->data.cgrade=numberinput("C language score[0-100]:"); newinfo->data.mgrade=numberinput("Math score[0-100]:"); newinfo->data.egrade=numberinput("English score[0-100]:"); newinfo->data.total=newinfo->data.cgrade+newinfo->data.mgrade+newinfo->data.egrade; newinfo->data.ave=(float)(newinfo->data.total/3); newinfo->data.mingci=0; newinfo->next=NULL; saveflag=1; p=l->next; while(1) { if(strcmp(p->data.num,s)==0) { newinfo->next=p->next; p->next=newinfo; break; } p=p->next; } disp(l); printf("\n\n"); getchar(); } void tongji(link l) { node *pm,*pe,*pc,*pt; node *r=l->next; int countc=0,countm=0,counte=0; if(!r) { system("cls"); printf("\n=====>not student record!\n"); getchar(); return; } system("cls"); disp(l); pm=pe=pc=r; while(r) { if(r->data.cgrade<60) countc++; if(r->data.egrade<60) counte++; if(r->data.mgrade<60) countm++; if(r->data.cgrade>=pc->data.cgrade) pc=r; if(r->data.egrade>=pc->data.egrade) pe=r; if(r->data.mgrade>=pc->data.mgrade) pm=r; if(r->data.total>=pc->data.total) pt=r; r=r->next; } printf("\n-----------the tongji result-------------------------------\n"); printf("C language<60:%d(ren)\n",countc); printf("Math <60:%d(ren)\n",countm); printf("English <60:%d(ren)\n",counte); printf("-------------------------------------------------------------\n"); printf("The highest student by total score name:%s total score:%d\n", pt->data.name,pt->data.total); printf("The highest student by English score name:%s total score:%d\n", pe->data.name,pe->data.egrade); printf("The highest student by Math score name:%s total score:%d\n", pm->data.name,pm->data.mgrade); printf("The highest student by C score name:%s total score:%d\n", pc->data.name,pc->data.cgrade); printf("\n\npress any key to return"); getchar(); } void sort(link l) { link ll; node *p,*rr,*s; int i=0; if(l->next==NULL) { system("cls"); printf("\n=====>not student record!\n"); getchar(); return; } ll=(node*)malloc(sizeof(node)); if(!ll) { printf("\n allocate memory failure!"); return; } ll->next=NULL; system("cls"); disp(l); p=l->next; while(p) { s=(node *)malloc(sizeof(node)); if(!s) { printf("\n allocate memory failure!"); return; } s->data=p->data; s->next=NULL; rr=ll; while(rr->next!=NULL&&rr->next->data.total>=p->data.total) { rr=rr->next;} if(rr->next==NULL) rr->next=s; else { s->next=rr->next; rr->next=s; } p=p->next; } l->next=ll->next; p=l->next; while(p!=NULL) { i++; p->data.mingci=i; p=p->next; } disp(l); saveflag=1; printf("\n =====>sort complete!\n"); } void save(link l) { FILE *fp; node *p; int count=0; fp=fopen("student.txt","wb"); if(fp==NULL) { printf("\n=====>open file error!\n"); getchar(); return; } p=l->next; while(p) { if(fwrite(p,sizeof(node),1,fp)==1) { p=p->next; count++; } else { break; } } if(count>0) { getchar(); printf("\n\n\n\n\n=====>save file complete,total saved's record number is:%d\n",count); getchar(); saveflag=0; } else { system("cls"); printf("the current link is empty,no student record is saved!\n"); getchar(); } fclose(fp); } int main() { link l; FILE *fp; int select; char ch; int count=0; node *p,*r; l=(node *)malloc(sizeof(node)); if(!l) { printf("\nallocate memor failure "); return 0; } l->next=NULL; r=l; fp=fopen("student.txt","ab+"); if(fp==NULL) { printf("\n======>can not open file!\n"); exit(0); } do { p=(node *)malloc(sizeof(node)); if(!p) { printf("memory malloc failure!\n"); exit(0); } if(fread(p,sizeof(node),1,fp)==1) { p->next=NULL; r->next=p; r=p; count++; } }while(!feof(fp)); fclose(fp); printf("\n======>open file success,the total records number is : %d.\n",count); getchar(); menu(); while(1) { system("cls"); menu(); p=r; printf(" Please enter your choice(0~9): "); scanf("%d",&select); if(select==0) { if(saveflag==1) { getchar(); printf("\n======>Whether save the modified record to file?(y/n)"); scanf("%c",&ch); if(ch=='y'||ch=='Y') save(l); } printf("=======>thank you for useness!"); getchar(); break; } switch(select) { case 1:add(l);break; case 2:del(l);break; case 3:qur(l);break; case 4:modify(l);break; case 5:insert(l);break; case 6:tongji(l);break; case 7:sort(l);break; case 8:save(l);break; case 9:system("cls");disp(l);break; default:wrong();getchar();break; } } return 0; }
[ 本帖最后由 sxwsxw 于 2010-8-11 12:54 编辑 ]