高手请教:链表读入读出文件出现乱码
#include <stdio.h>#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#define LEN sizeof(struct shares)
#define NUM 3
struct shares
{
char name[20];
char num[20];
int date;
long price;
char sort[15];
char company[20];
struct shares * next;
};
int n=0;
struct shares * input()// 录入函数//
{
struct shares * head=0;
struct shares *p1,*p2;
while(n<NUM)
{
p1=(struct shares *)malloc(LEN);
scanf("%s %s %d %ld %s %s",p1->name,p1->num,&p1->date,&p1->price,p1->sort,p1->company);
n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
}
p2->next=0;
return(head);
}
void save(struct shares * head) //保存记录//
{
FILE * fp;
struct shares *p;
p=head;
int i;
if((fp=fopen("data.txt","wb"))==0)
{
printf("cannot open the file\n");
return;
}
for(i=0;(p+i)->next!=0;i++)
if(fwrite((p+i),LEN,1,fp)!=1) printf("file write error\n");
fclose(fp);
}
void read(struct shares *head) //读取数据//
{
int i;
struct shares *pp;
pp=head;
FILE * fp;
fp=fopen("data.txt","rb");
for(i=0;pp->next!=0;i++)
{
fread(pp,LEN,1,fp);
printf("\n%s %s %d %ld %s %s\n",pp->name,pp->num,pp->date,pp->price,pp->sort,pp->company);
pp++;
}
}
void main()
{
struct shares * head;
head=input();
save(head);
read(head);
exit(0);
}
随便输入数据 aa bb 33 33 dd gg
bb ff 33 88 ff ss
dd ff 33 99 ff gg
保存txt文件后就会出现很多很多屯啥的,读出数据时显示的也是
是哪个地方出问题了呢?哪位高手指点迷津吧!小弟在此谢谢了