求高手指导文件读取问题
程序代码:
#include<stdio.h> #include<stdlib.h> #include<malloc.h> #define TYPE struct Chose #define endinput printf("输入0,0结束") TYPE { int num; float price; TYPE * next; }; int n; void menu() { printf("提示:\n"); printf("1.首次输入:\n"); printf("2.补充输入:\n"); printf("3.存盘:\n"); printf("4.读取:\n"); printf("5.删除:\n"); printf("6.修改:\n"); printf("7.退出:\n"); } void save(TYPE * head) { FILE * fp; TYPE * p; if((fp=fopen("mk.txt","w"))==NULL) { printf("cannot open infile\n"); exit(0); } for(p=head;p!=NULL;p=p->next) { if((fprintf(fp,"%d,%f",p->num,p->price)!=1)) printf("file write error\n"); } fclose(fp); } void load(TYPE * head) { FILE * fp; TYPE u[20]; TYPE * p; int i; if((fp=fopen("mk.txt","r"))==NULL) { printf("cannot open infile\n"); exit(0); } p=head; for(i=0;i<100;i++) { fscanf(fp,"%d,%f",&u[i].num,&u[i].price); if(u[i].num='\0') break; p->num=u[i].num; p->price=u[i].price; p=p->next; } p->next=NULL; } /*TYPE * load() { FILE * fp; TYPE * p,* q,* head; int c=0; if((fp=fopen("mk.txt","r"))==NULL) { printf("cannot open infile\n"); exit(0); } q=p=(TYPE *)malloc(sizeof (TYPE)); fscanf(fp,"%d,%f",&q->num,&q->price); printf("1"); head=NULL; while(q->num!=0) { c=c+1; if(c==1) head=q; else p->next=q; p=q; q=(TYPE *)malloc(sizeof (TYPE)); fscanf(fp,"%d,%f",&q->num,&q->price); } p->next=NULL; fclose(fp); return(head); }*/ /* for(p=head;p!=NULL;p=p->next) { if((fscanf(fp,"%d号菜,价格:%f\n",p->num,p->price)!=1)) printf("file write error\n"); fclose(fp); } */ TYPE * creat() { TYPE * head; TYPE * p1,* p2; n=0; p1=p2=(TYPE *)malloc(sizeof (TYPE)); scanf("%d,%f",&p1->num,&p1->price); head=NULL; while(p1->num!=0) { n=n+1; if(n==1) head=p1; else p2->next=p1; p2=p1; p1=(TYPE *)malloc(sizeof (TYPE)); scanf("%d,%f",&p1->num,&p1->price); } p2->next=NULL; return(head); } void main() { TYPE *p,*t,*head,*q; int i=1,k; float b; while(i) { menu(); scanf("%d",&i); switch(i) { case 1: printf("输入菜的编码和价格(num,price)\n"); //endinput; head=creat(); for(p=head;p!=NULL;p=p->next) { printf("%d号菜,价格:%f\n",p->num,p->price); } break; case 2: printf("补充菜的编码与价格(num,price)\n"); for(p=head;p!=NULL;p=p->next) { if(p->next==NULL) break; } t=(TYPE * )malloc(sizeof (TYPE)); scanf("%d,%f",&t->num,&t->price); p->next=t; p=t; p->next=NULL; for(p=head;p!=NULL;p=p->next) { printf("%d号菜,价格:%f\n",p->num,p->price); } break; case 3:save(head); //save(); break; case 4:load(head); for(p=head;p!=NULL;p=p->next) { printf("%d,%f\n",p->num,p->price); } break; case 5: printf("需要删除的菜的编号(num)\n"); scanf("%d",&k); for(p=head;p!=NULL;q=p,p=p->next) { if(p->num==k) break; } if(p->next!=NULL) { q->next=p->next; free(p); } else { q->next=NULL; free(p); } for(p=head;p!=NULL;p=p->next) { printf("%d号菜,价格:%f\n",p->num,p->price); } break; case 6: printf("需要修改的菜和的编号(num)\n"); scanf("%d",&k); printf("需要修改的菜和的价格(price)\n"); scanf("%f",&b); for(p=head;p!=NULL;p=p->next) { if(p->num==k) break; } p->price=b; for(p=head;p!=NULL;p=p->next) { printf("%d号菜,价格:%f\n",p->num,p->price); } break; case 7: printf("退出\n"); i=0; break; default: printf("没有此选项~\(≧▽≦)/~啦啦啦\n"); } } printf("欢迎使用,再见!=v=\n"); }
我写了一个菜单程序,发现存入文件和读取文件时总是发生错误,不知道错在哪,求各位高手大大帮忙。。。。
程序写得很烂。。。求包涵。。。
[ 本帖最后由 腹黑伸手党 于 2011-12-30 21:44 编辑 ]