链表信息怎么输入文件

neee333 2017-07-10 04:14:22
我照这种方法输入,结果文件打开时乱码,究竟啥情况
#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等字样

...全文
325 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-07-10
  • 打赏
  • 举报
回复
两种思路: ①next不写入文件,仅将数据按next顺序写入文件。读取时重建next ②next写入文件时,翻译为文件偏移量。读取时按照文件中的偏移量随机读取每个数据。 以楼主的程度,选择①
neee333 2017-07-10
  • 打赏
  • 举报
回复
引用 5 楼 cfjtaishan 的回复:
写文件是二进制的,打开文件时,自然是乱码;若是用fscanf/fprintf读写文件,打开就不应该是乱码了。 楼主你的逻辑有问题呢,首先,开户的时候已经将用户信息写到文件里,并且开户信息已经在内存里了,head头节点。为什么还要在登录的函数里再从文件里读呢。head链表已经有用户的开户信息了。操作重复了,建议开户只是向文件里写,不创建链表,登录从文件里读。 正常的逻辑是,开户写文件和创建链表(或插入节点),登录用开户创建的链表进行比较。当程序再次启动时,建立一个函数专门是从文件里读用户的开户信息,创建链表。这个链表头传给登录操作,用于匹配用户信息。
那next怎么写入文件呢,对链表不是特别熟,能教教我吗
neee333 2017-07-10
  • 打赏
  • 举报
回复
那next怎么写入文件呢,对链表不是特别熟,能教教我吗
自信男孩 2017-07-10
  • 打赏
  • 举报
回复
写文件是二进制的,打开文件时,自然是乱码;若是用fscanf/fprintf读写文件,打开就不应该是乱码了。 楼主你的逻辑有问题呢,首先,开户的时候已经将用户信息写到文件里,并且开户信息已经在内存里了,head头节点。为什么还要在登录的函数里再从文件里读呢。head链表已经有用户的开户信息了。操作重复了,建议开户只是向文件里写,不创建链表,登录从文件里读。 正常的逻辑是,开户写文件和创建链表(或插入节点),登录用开户创建的链表进行比较。当程序再次启动时,建立一个函数专门是从文件里读用户的开户信息,创建链表。这个链表头传给登录操作,用于匹配用户信息。
neee333 2017-07-10
  • 打赏
  • 举报
回复
我把开户文件改了一下,数据存入没问题了,但后面文件完全不能运行了 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","w+"))==NULL) //为读写打开一个文本文件 { printf("cannot open file\n"); return; } fprintf(fp,"%s %s %d %s %d",p1->name,p1->ID,p1->money,p1->code,p1->next); fclose(fp); printf("\n"); printf("恭喜您开户成功,请登录\n"); system("pause"); system("cls"); denglu(head); } 怎么肥四啊
赵4老师 2017-07-10
  • 打赏
  • 举报
回复
对电脑而言没有乱码,只有二进制字节;对人脑才有乱码。
赵4老师 2017-07-10
  • 打赏
  • 举报
回复
电脑内存或文件内容或传输内容只是一个一维二进制字节数组及其对应的二进制地址; 人脑才将电脑内存或文件内容或传输内容中的这个一维二进制字节数组及其对应的二进制地址的某些部分看成是整数、有符号数/无符号数、浮点数、复数、英文字母、阿拉伯数字、中文/韩文/法文……字符/字符串、汇编指令、函数、函数参数、堆、栈、数组、指针、数组指针、指针数组、数组的数组、指针的指针、二维数组、字符点阵、字符笔画的坐标、黑白二值图片、灰度图片、彩色图片、录音、视频、指纹信息、身份证信息……
赵4老师 2017-07-10
  • 打赏
  • 举报
回复
next的值在内存中是内存地址,在文件中是文件偏移量。

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧