来个大神帮我看看,输入输出哪里错了?记录不了信息
#include<stdio.h>#include<stdlib.h>
#include<string.h>
int n=0; //定义一个全局变量n
//定义结构体
struct huiyuan
{
int kahao; //卡号
char xingming[15]; //姓名
int dengji; //会员等级
float zhekoulv; //会员折扣率
};struct huiyuan huiyuan[9]; //结构体变量数组
//定义菜单
int caidan()
{
int m;
printf("欢迎使用博文产品\n\n\n\n\n\n");
printf("\t************************主页**************************\n");
printf("\t* 1.会员卡信息录入功能; *\n");
printf("\t* 2.会员卡信息整体预览功能; *\n");
printf("\t* 3.按卡号查询会员卡信息; *\n");
printf("\t* 4.按输入的“会员等级”统计该等级会员卡的数量信息;*\n");
printf("\t* 5.修改某一“会员等级”的折扣率的值的信息; *\n");
printf("\t* 6.按照输入的“会员姓名”信息删除该会员信息; *\n");
printf("\t* 7.根据“折扣率”由小到大排序; *\n");
printf("\t* 8.退出系统; *\n");
printf("\t******************************************************\n");
scanf("%d",&m);
return m;
}
//会员卡信息录入功能
void lvru()
{
FILE *p;
if((p=fopen("shuju.dat","ab"))==NULL) //打开“shuju.dat”文件并判断是否成功打开
{
printf("无法打开/建立文件");
return;
}
fseek(p,n*sizeof(struct huiyuan),0); //指定指针当前指向位置
int a;
for(n;;n++) //用for语句重复录入会员信息
{
printf("请输入会员信息\n");
printf("\t卡号\t姓名\t会员等级\t折扣率\t\n");
printf("\n请输入卡号:");
scanf("%d",&huiyuan[n].kahao);
printf("\n请输入姓名:");
scanf("%s",huiyuan[n].xingming);
printf("\n请输入等级:");
scanf("%d",&huiyuan[n].dengji);
printf("\n请输入折扣率:");
scanf("%f",&huiyuan[n].zhekoulv);
if(fwrite(&huiyuan[n],sizeof(struct huiyuan),1,p)!=1) //将信息写入“shuju.dat”文件中并判断是否成功写入
{
printf("执行失败");
return;
}
printf("\n继续录入会员请按1,否则请按任意键");
scanf("%d",&a);
if(a!=1)
{
fclose(p); //关闭文件“shuju.dat”
break; //跳出循环
}
}
n++; //用break强制跳出循环时最后一次不执行n++,所以这里给他加上
printf("请输入任意键回到菜单\n");
scanf("%d",&a);
}
//会员卡信息整体预览功能
void yulan()
{
FILE *p;
if((p=fopen("shuju.dat","rb"))==NULL) //只读方式打开文件并判断是否正常打开
{
printf("无法打开文件");
exit(0);
}
int a,b;
printf("\t卡号\t姓名\t会员等级\t折扣率\t\n");
for(a=0;a<n;a++)
{
fread(&huiyuan[a],sizeof(struct huiyuan),1,p); //将文件中的数据一个结构体一个结构体的赋值到结构体变量数组上
printf("\t%d\t%s\t%d\t\t%0.2f\n",huiyuan[a].kahao,huiyuan[a].xingming,huiyuan[a].dengji,huiyuan[a].zhekoulv);
}
fclose(p); //使用完后关闭文件
printf("请输入任意键回到菜单\n");
scanf("%d",&b);
}
//按卡号查询会员卡信息
void chaxun()
{
FILE *p;
if((p=fopen("shuju.dat","rb"))==NULL) //只读方式打开文件并判断是否正常打开
{
printf("无法打开该文件");
exit(0);
}
int a,b;
printf("请输入卡号");
scanf("%d",&b);
for(a=0;a<n;a++) //用for语句把文件的数据赋给结构体变量数组,并找出所查找会员的信息,然后跳出循环
{
fread(&huiyuan[a],sizeof(struct huiyuan),1,p);
if(b==huiyuan[a].kahao)
{
printf("\t卡号\t姓名\t会员等级\t折扣率\t\n");
printf("\t%d\t%s\t%d\t\t%0.2f\n",huiyuan[a].kahao,huiyuan[a].xingming,huiyuan[a].dengji,huiyuan[a].zhekoulv);
}
if(b==huiyuan[a].kahao)break;
}
if(a==n)
printf("\n查无此人");
fclose(p); //关闭文件
printf("\n请输入任意输回到菜单");
scanf("%d",&a);
}
//按输入“会员等级”统计该等级会员卡的数量信息
void tongji()
{
FILE *p;
if((p=fopen("shuju.dat","rb"))==NULL)
{
printf("无法打开该文件");
exit(0);
}
int a,b,c=0;
printf("请输入会员卡等级:");
scanf("%d",&a); //a记录所查找的等级,c记录该会员等级总人数
printf("\t卡号\t姓名\t会员等级\t折扣率\t\n");
for(b=0;b<n;b++)
{
fread(&huiyuan[b],sizeof(struct huiyuan),1,p);
if(a==huiyuan[b].dengji)
{
printf("\t%d\t%s\t%d\t\t%0.2f\n",huiyuan[b].kahao,huiyuan[b].xingming,huiyuan[b].dengji,huiyuan[b].zhekoulv);
c++;
}
}
fclose(p);
printf("该等级共有%d\n",c);
printf("请输入任意键回到菜单\n");
scanf("%d",&a);
}
//修改某一“会员等级”的折扣率的值的信息;
void xiugai()
{
FILE *p;
if((p=fopen("shuju.dat","rb"))==NULL)
{
printf("无法打开该文件");
exit(0);
}
int a,b=0,c,e=-1;
float d,f;
printf("请输入所需修改的扣率的等级:");
scanf("%d",&a);
fread(&huiyuan[0],sizeof(struct huiyuan),1,p);
while(a!=huiyuan[b].dengji) //用循环一一给结构体变量数组赋值,并找出该等级会员卡当前折扣率
{
b++;
fread(&huiyuan[b],sizeof(struct huiyuan),1,p);
}
fclose(p);
printf("该会员等级当前扣率为:%f\n",huiyuan[b].zhekoulv);
printf("确认修改请按1,返回主菜单请输入其他数:");
scanf("%d",&c);
if((p=fopen("shuju.dat","ab+"))==NULL)
{
printf("无法打开文件");
return;
}
if(c==1)
{
printf("请输入新扣率:");
scanf("%f",&d);
for(e=0;e<n;e++)
{
fseek(p,e*sizeof(struct huiyuan),0); //重新定义指针位置
fread(&huiyuan[e],sizeof(struct huiyuan),1,p); //定义完后开始对结构体变量数组赋值
if(a==huiyuan[e].dengji)
{
huiyuan[e].zhekoulv=d; //找出该等级的人并逐一修改折扣率
fwrite(&huiyuan[e],sizeof(struct huiyuan),1,p); //修改完后重新写入文件
}
}
}
printf("查看当前该等级扣率请输入1,返回主菜单请输入其他数:");
scanf("%f",&f);
if(f==1)
{
e=-1;
do
{
e++;
fseek(p,e*sizeof(struct huiyuan),0);
fread(&huiyuan[e],sizeof(struct huiyuan),1,p);
}while(a!=huiyuan[e].dengji);
fclose(p);
printf("%f\n",huiyuan[e].zhekoulv);
printf("按任意键返回主菜单:");
scanf("%d",&a);
}
}
//按照输入的“会员姓名”信息删除该会员信息;
void shanchu()
{
FILE *p;
if((p=fopen("shuju.dat","ab+"))==NULL)
{
printf("无法打开文件");
exit(0);
}
rewind(p);
int a=-1,b,c,x;
char d[15];
printf("请输入会员名字:");
scanf("%s",d);
for(x=0;x<n;x++)
fread(&huiyuan[x],sizeof(struct huiyuan),1,p);
printf("该会员信息为\n");
do //找出该会员并记录该会员所在“位置”
{
a++;
if(a>=n)
{
printf("输入有误,请重新输入:");
scanf("%s",d);
a=-1;
}
}while(strcmp(huiyuan[a].xingming,d)!=0);
printf("\t卡号\t姓名\t会员等级\t折扣率\t\n");
printf("\t%d\t%s\t%d\t\t%0.2f\n",huiyuan[a].kahao,huiyuan[a].xingming,huiyuan[a].dengji,huiyuan[a].zhekoulv);
printf("\n\n确认删除请输入1,取消删除返回主菜单请输入其他数:");
scanf("%d",&b);
if(b==1)
{
for(a;a<n;a++) //用覆盖的方法删除该会员信息,并使后面的会员往前移中间不留下空隙
{
huiyuan[a].kahao=huiyuan[a+1].kahao;
*huiyuan[a].xingming=*huiyuan[a+1].xingming;
huiyuan[a].dengji=huiyuan[a+1].dengji;
huiyuan[a].zhekoulv=huiyuan[a+1].zhekoulv;
}
n--;
rewind(p);
for(a=0;a<n;a++)
fwrite(&huiyuan[a],sizeof(struct huiyuan),1,p);
fclose(p);
printf("删除完毕,请输入任意数返回主菜单:");
scanf("%d",&c);
}
}
//根据“折扣率”由小到大排序
void paixu()
{
FILE *p;
if((p=fopen("shuju.dat","ab+"))==NULL)
{
printf("无法打开文件");
exit(0);
}
rewind(p);
int a,b,c,e,x;
for(x=0;x<n;x++)
fread(&huiyuan[x],sizeof(struct huiyuan),1,p);
rewind(p);
float d;
char q[15];
for(a=0;a<n;a++)
{
for(b=0;b<n-a;b++)
{
if(huiyuan[a].zhekoulv>huiyuan[b].zhekoulv)
{
c=huiyuan[a].kahao;huiyuan[a].kahao=huiyuan[b].kahao;huiyuan[b].kahao=c;
*q=*huiyuan[a].xingming;*huiyuan[a].xingming=*huiyuan[b].xingming;*huiyuan[b].xingming=*q;
c=huiyuan[a].dengji;huiyuan[a].dengji=huiyuan[b].dengji;huiyuan[b].dengji=c;
d=huiyuan[a].zhekoulv;huiyuan[a].zhekoulv=huiyuan[b].zhekoulv;huiyuan[b].zhekoulv=d;
}
}
}
for(x=0;x<n;x++)
fwrite(&huiyuan[x],sizeof(struct huiyuan),1,p);
fclose(p);
printf("排序已完成,查看列表请输入1,返回主菜单请输入其他数:");
scanf("%d",&e);
if(e==1)
yulan();
}
void tuichu()
{
printf("\n\n\t\t谢谢使用!!!\n\t\t 再见!\n");
exit(0);
}
void main()
{
int o,r;
printf("是否建立新档案,是请输入1,不是请输入其他数:");
scanf("%d",&r);
if(r==1)
{
FILE *p;
if((p=fopen("shuju.dat","wb"))==NULL)
{
printf("建立失败");
exit(0);
}
fclose(p);
}
while(1)
{
o=caidan();
switch(o)
{
case 1:lvru();break;
case 2:yulan();break;
case 3:chaxun();break;
case 4:tongji();break;
case 5:xiugai();break;
case 6:shanchu();break;
case 7:paixu();break;
case 8:tuichu();break;
}
}
}