我照这种方法输入,结果文件打开时乱码,究竟啥情况
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct per //定义结构体
{
char name[20];
char ID[20];
int money;
char code[20];
struct per * next;
};
void caidan(struct per *head);//菜单函数
void chaxun(struct per *head);//查询函数
void kaihu(struct per *head);//开户函数
void denglu(struct per *head);//登陆函数
void qukuan(struct per *head);//取款函数
void xgmm(struct per *head);//修改密码函数
void cunkuan(struct per *head);//存款函数
void zhuanzhang(struct per *head);//转账函数
void tuichu();//退出函数
void main()//主函数
{
char x;
struct per *head=NULL; //head为定义一层头指针
printf("——————————————\n");
printf("| 欢迎使用银行自动取款机系统 |\n");
printf("——————————————\n");
printf("——————————————\n");
printf("| 1 开户 |\n");
printf("——————————————\n");
printf("| 2 登陆 |\n");
printf("——————————————\n");
printf("| 3 退出 |\n");
printf("——————————————\n");
scanf("%s",&x);
system("cls");
switch(x)
{
case '1':system("cls");
kaihu(head); //调用开户函数
break;
case '2':system("cls");
denglu(head); //调用登陆函数
break;
case '3':system("cls");
tuichu();
}
}
void kaihu(struct per *head)//开户函数
{
head=NULL;
FILE *fp; //定义文件指针
struct per *p1=NULL,*p2=NULL; //p1,p2为定义链表指针
p1=(struct per*)malloc(sizeof(struct per)); //开辟内存单元
printf("请输入您的姓名:\n"); //请数据输入链表中
scanf("%s",p1->name);
printf("请设置您的卡号:\n");
scanf("%s",p1->ID);
printf("请设置您银行卡密码:\n");
scanf("%s",p1->code);
p1->money=0;
p1->next=NULL;
printf("您的个人信息为");
printf("姓名:%s \n卡号:%s \n余额:%d\n",p1->name,p1->ID,p1->money);
if(NULL==head) //为新用户开辟内存单元
{
head=(struct per *)malloc(sizeof(struct per));
head->next=p1; //进行头插法,将其作为第一个节点
}
else //为新增客户开辟内存单元
{
for(p2=head;p2->next!=NULL;p2=p2->next); //进行尾插
p2->next=p1;
}
if((fp=fopen("infor.txt","ab+"))==NULL) //为读写打开一个二进制文件
{
printf("cannot open file\n");
return;
}
if(fwrite(p1,sizeof(struct per),1,fp)!=1) //将链表信息写入文件中
printf("file write error\n");
fclose(fp);
printf("\n");
printf("恭喜您开户成功,请登录\n");
system("pause");
system("cls");
denglu(head);
}
void denglu(struct per *head)//登陆函数
{
char d[20];
char code[20];
int i,j;
FILE *fp; //定义文件指针
struct per *p,*q=NULL;
if((fp=fopen("infor.txt","rb+"))==NULL) //为了读和写,打开一个二进制文件
{
printf("cannot open file\n"); //如不能打开,则结束程序
}
p=(struct per*)malloc(sizeof(struct per)); //申请空间
head=p;
while(!feof(fp)) //循环读数据直到文件尾结束
{
if(1!=fread(p,sizeof(struct per),1,fp))
break; //如果没读到数据,跳出循环
p->next=(struct per *)malloc(sizeof(struct per)); //为下一个结点申请空间
q=p; //保存当前节点的指针,作为下一结点的前驱
p=p->next; //指针后移,新读入数据链到当前表尾
}
q->next=NULL; //最后一个结点的后继指针为空
fclose(fp);
printf("—————————\n");
printf("|欢迎使用建设银行|\n");
printf("—————————\n");
for(i=0;i<3;i++) //限制卡号输入的次数的循环
{
printf("请输入您的卡号\n");
scanf("%s",d);
for(q=head;q!=NULL;q=q->next) //遍历链表
{
if(strcmp(q->ID,d)!=0) //核对账号
{
continue; //跳出循环
}
else
{
for(j=0;j<3;j++) //限制密码输入的次数的循环
{
printf("\n\n请输入您的密码\n");
scanf("%s",code);
if(strcmp(q->code,code)!=0) //核对密码
{
printf("密码不正确。请重新输入密码\n");
system("pause");
system("cls");
continue; //若密码不对,跳出循环
}
else
{
system("cls");
caidan(head); //调用菜单函数
}
}
printf("\n\n\n您输入密码三次错误,谢谢光临\n");
system("pause");
system("cls");
exit(0);
}
}
printf("\n\n\n您输入的卡号有误,请重试\n");
system("pause");
system("cls");
}
printf("您的卡号三次输入错误,谢谢使用\n");
exit(0);
}
void caidan(struct per *head)//ATM机菜单函数
{
head=NULL;
int i; //i为客户选择输入的变量
while(1)
{
printf("请选择您需要的业务\n"); //ATM机业务菜单
printf("1 取款\n2 存款\n3 查询\n4 转账\n5 修改密码\n6退出\n");
scanf("%d",&i);
if(i<7&&i>0)
{
switch(i)
{
case 1:qukuan(head); //调用ATM机取款函数
system("pause");
system("cls");
break;
case 2:system("cls");
cunkuan(head); //调用ATM机查询函数
break;
case 3:system("cls");
chaxun(head); //调用ATM机转账函数
break;
case 4:system("cls");
zhuanzhang(head); //调用ATM机修改密码函数
break;
case 5:system("cls");
xgmm(head); //调用ATM机存款函数
break;
case 6:system("cls");
tuichu(); //调用ATM机退出函数
break;
}
}
else
{
printf("您的输入有误\n");
system("pause");
system("cls");
}
}
}
字数限制就到这,主要是开户函数输入的数据,文件中是乱码,而且出现system32等字样