奇怪的C语言错误

这段程序,首先是实现输入一个结构体,然后输出到文件,结果写了个读的函数(read)后执行输入不了了,我在主函数中没有引用read如果把read函数用/**/屏蔽掉的话有恢... 这段程序,
首先是实现输入一个结构体,然后输出到文件,
结果写了个读的函数(read)后执行输入不了了,我在主函数中没有引用read
如果把read函数用/* */屏蔽掉的话有恢复正常了,这是怎么回事??
#include<stdio.h>
/* #include<malloc.h>*/
#include<string.h>
#define LEN sizeof(struct info)
#define Malloc (struct info*)malloc(LEN)
#define NULL 0

struct info
{
char name[20];
char addr[40];
char mailnum[10];
struct info *next;
};
main()
{
void print(struct info *head);
struct info *creat();
struct info *read(FILE *fp);
struct info *p,*p1,*p2;
FILE *fp;
struct info *head;
fp=fopen("c:\\info.txt","wb+");
head=creat();
p=head;
while(p!=NULL)
{printf("%s %s %s\n",p->name,p->addr,p->mailnum);
fwrite(p,LEN,1,fp);
p=p->next;
}
/*
head=read(fp);
print(head); */
fclose(fp);

}
void print(struct info *head)
{
struct info *p;
p=head;
while(p!=NULL)
{
printf("%s %s %s",p->name,p->addr,p->mailnum);
p=p->next;
}
}

struct info *read(FILE *fp)
{
struct info *head,*p1,*p2;
head=NULL;
p2=p1=Malloc;
while(!feof(fp))
{
fread(p1,LEN,1,fp);
if(p2==p1) head=p1;
else p2->next=p1;
p2=p1;
p1=Malloc;
}
p2->next=NULL;
return(head);
}

struct info *creat()
{
struct info *head,*p1,*p2;
p2=p1=Malloc;
head=NULL;
printf("please input info\n");
getchar();
scanf("%s %s %s",p1->name,p1->addr,p1->mailnum);
while(strcmp(p1->name,"0")!=0)
{
if(p2==p1) head=p1;
else p2->next=p1;
p2=p1;
p1=Malloc;
scanf("%s %s %s",p1->name,p1->addr,p1->mailnum);
}
p2->next=NULL;
return(head);
}

展开
 我来答
bdlanyu
2009-10-26 · TA获得超过550个赞
知道小有建树答主
回答量:378
采纳率:0%
帮助的人:537万
展开全部
在read前面加上
seek(fp,0,SEEK_SET);
文件指针指向文件的读写位置,
你写入文件后,指针在文件末.
或者可以关闭文件后再读取.
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式