我写了一个“家庭收支管理系统”,程序读取文件有问题,我找不出出错在哪,希望哪位高手帮帮忙?谢谢了!
我写了一个“家庭收支管理系统”,程序读取文件有问题,我找不出出错在哪,希望哪位高手帮帮忙?谢谢了!程序代码:
#include<stdio.h> #include<stdlib.h> #include<string.h> #define SIZE 10 struct people { char name[20]; int year; int month; int in; int out; }peo[SIZE]; int n=0,m=0; int save() { FILE *fp; int i; fp=fopen("D:people.txt","w"); { fprintf(fp,"%d\n",n); for(i=0;i<n;i++) fprintf(fp,"%s\n",peo[i].name); fprintf(fp,"%d\n",peo[i].year); fprintf(fp,"%d\n",peo[i].month); fprintf(fp,"%d\n",peo[i].in); fprintf(fp,"%d\n",peo[i].out); } fclose(fp); return 0; } int read() { FILE *fp; int i; if((fp=fopen("D:people.txt","r"))!=NULL); { fscanf(fp,"%d\n",n); for(i=0;i<n;i++) fscanf(fp,"%s\n",peo[i].name); fscanf(fp,"%d\n",peo[i].year); fscanf(fp,"%d\n",peo[i].month); fscanf(fp,"%d\n",peo[i].in); fscanf(fp,"%d\n",peo[i].out); } fclose(fp); return 0; } int add_people() { int save(); char name[20]; printf("\n[录入新成员信息]\n"); printf("\n请输入姓名:"); scanf("\n%s",name); strcpy(peo[m].name,name); printf("\n请输入年份:"); scanf("%d",&peo[m].year); printf("\n请输入月份:"); scanf("%d",&peo[m].month); printf("\n请输入收入:"); scanf("%d",&peo[m].in); printf("\n请输入支出:"); scanf("%d",&peo[m].out); save(); return 0; } int dele_people() { FILE *fp; char name[20]; int i,k,g,h,p=0; printf("\n[删除成员信息]\n"); printf("\n请输入姓名:"); scanf("\n%s",name); read(); h=n; for(i=0;i<m;i++) { if(strcpy(peo[i].name,name)==0) { k=1;break; } } if(i==n) k=0; if(k==0) printf("\n不存在该成员\n"); if(k==1) { for(i=0;i<n;i++) if(strcpy(peo[i].name,name)==0) { g=i;p=1;break; } if(p==1) { n=h-1; fp=fopen("people","w"); fprintf(fp,"%d\n",n); for(i=0;i<g;i++) fprintf(fp,"%s%d%d%d%d\n",peo[i].name,peo[i].year, peo[i].month, peo[i].in, peo[i].out); for(i=g+1;i<h;i++) fprintf(fp,"%s%d%d%d%d\n",peo[i].name,peo[i].year, peo[i].month, peo[i].in, peo[i].out); fclose(fp); printf("\n••••••••\n删除成功\n"); } } save(); return 0; } int change_people() { char name[20],name1[20]; int i,year,month,year1,month1,in,out,k,p=0,t=0; printf("\n[修改成员收支信息]\n\n请输入姓名:"); scanf("%s",name); read(); for(i=0;i<m;i++) { if(strcmp(peo[i].name,name)==0) { k=1;break; } } if(i==m) k=0; if(k==0) printf("\n不存在该成员\n"); if(k==1) { printf("\n以下为%s收支信息:\n\n年 月 收 支\n",name); for(i=0;i<m;i++) { printf("%4d%4d%9d%9d",peo[i].year,peo[i].month,peo[i].in, peo[i].out); } printf("\n请输入需要修改的具体时间:\n年 月\n"); scanf("%d%d",&year,&month); for(i=0;i<m;i++) { if(peo[i].year==year&& peo[i].month==month) { p=1;break; } } if(p==0) printf("\n没有该时间的收支情况信息\n\n修改失败\n"); if(p==1) { printf("\n请输入修改后数据:\n"); printf("\n姓名:"); scanf("%s",name1); printf("\n年份"); scanf("%d",year1); printf("\n月份"); scanf("%d",month1); printf("\n收入"); scanf("%d",in); printf("\n支出"); scanf("%d",out); } for(i=0;i<m;i++) if(strcmp(peo[i].name,name)==0&&peo[i].year==year&&peo[i].month==month) { strcpy(peo[i].name,name1); peo[i].year=year1; peo[i].month=month; peo[i].in=in;peo[i],out=out; break; } printf("\n••••••••\n修改成功\n"); } save(); return 0; } int search_people() { int i,k; char name[20]; read(); printf("\n[查询成员收支信息]\n\n请输入姓名:"); scanf("%s",name); for(i=0;i<m;i++) { if(strcmp(peo[i].name,name)==0) { k=1;break; } } if(i==m) k=0; if(k==0) printf("\n不存在该成员\n"); if(k==1) { printf("\n%s的所有收支信息:\n\n",name); printf("时间 收入 支出 总计\n"); for(i=0;i<m;i++) { if(strcmp(peo[i].name,name)==0&&peo[i].year!=0) printf("%d%d%9d%9d%9d\n",peo[i].year,peo[i].month, peo[i].in, peo[i].out, peo[i].in- peo[i].out); } } return 0; } int go() { printf("\n\n《谢谢使用家庭管理系统》\n\n"); printf("\n\n 潘黄岷\n\n"); exit(0); return 0; } void printmenu() { printf("\n 选择菜单\n"); printf("````````````````````````````````````````````````\n\n"); printf("1.添加成员收支信息\n"); printf("2.删除成员收支信息\n"); printf("3.修改成员收支信息\n"); printf("4.查询成员收支信息\n"); printf("5。退出界面\n\n"); printf("````````````````````````````````````````````````\n\n"); } void main() { int choise; while(1) { printf("\n"); printmenu(); printf("请输入你的选择\n"); scanf("%d", &choise); switch(choise) { case 1: printf("添加\n"); add_people(); break; case 2: printf("2 删除\n"); dele_people(); break; case 3: printf("3 修改\n"); change_people(); break; case 4: printf("4 查询\n"); search_people(); break; case 5: printf("8 退出\n"); go(); break; default: printf("输入错误!\n"); } } }