求助,C语言文件读取出错

时间:2013.11.28 发布人:dream810825

求助,C语言文件读取出错

已解决问题

谷歌dream810825用户在2013.11.28提交了关于“托业求助,C语言文件读取出错”的提问,欢迎大家涌跃发表自己的观点。目前共有1个回答,最后更新于2024-10-29T01:56:42。#include<stdio.h>
#include<malloc.h>
#include<string.h>


structaddress
{
inta;
intb;
structaddress*next;};


voidS**eToFile(structaddress*p,FILE*fp)
{
structaddress*L;
if((fp=fopen("C:\\addrbook","wb"))==**LL)
{
printf("文件不能打开\n");
return;
}
L=p->next;
if(L!=**LL)
{
while(L!=**LL)
{
fwrite(L,sizeof(structaddress),1,fp);
L=L->next;
}

}
fclose(fp);
}

intload(FILE*fp,structaddress**head)//读取文件**,建立链表
{
structaddress*p;

if((fp=fopen("C:\\addrbook","rb"))==**LL)
{
printf("!文件打开失败\n");
exit(1);
}

*head=(structaddress*)malloc(sizeof(structaddress));
(*head)->next=**LL;

while(!feof(fp))
{
p=(structaddress*)malloc(sizeof(structaddress));

fread(p,sizeof(structaddress),1,fp);
p->next=(*head)->next;
(*head)->next=p;

}
fclose(fp);
return0;
}



voidmain()
{
structaddress*L,*head;intn=0;inti=0;FILE*fp=**LL;structaddress*p;

L=(structaddress*)malloc(sizeof(structaddress));
L->next=**LL;
printf("输入ab的值\n");
p=(structaddress*)malloc(sizeof(structaddress));
scanf("%d%d",&p->a,&p->b);
while(p->a!=0)
{
p->next=L->next;
L->next=p;
p=(structaddress*)malloc(sizeof(structaddress));
printf("输入ab的值\n");
scanf("%d%d",&p->a,&p->b);
}

S**eToFile(L,fp);

load(fp,&head);
printf("**读取如下\n");
p=head->next;
while(p!=**LL)
{
printf("%d%d\n",p->a,p->b);
p=p->next;
}
}希望大家能够帮助她。

详细问题描述及疑问:#include<stdio.h>
#include<malloc.h>
#include<string.h>


structaddress
{
inta;
intb;
structaddress*next;};


voidS**eToFile(structaddress*p,FILE*fp)
{
structaddress*L;
if((fp=fopen("C:\\addrbook","wb"))==**LL)
{
printf("文件不能打开\n");
return;
}
L=p->next;
if(L!=**LL)
{
while(L!=**LL)
{
fwrite(L,sizeof(structaddress),1,fp);
L=L->next;
}

}
fclose(fp);
}

intload(FILE*fp,structaddress**head)//读取文件**,建立链表
{
structaddress*p;

if((fp=fopen("C:\\addrbook","rb"))==**LL)
{
printf("!文件打开失败\n");
exit(1);
}

*head=(structaddress*)malloc(sizeof(structaddress));
(*head)->next=**LL;

while(!feof(fp))
{
p=(structaddress*)malloc(sizeof(structaddress));

fread(p,sizeof(structaddress),1,fp);
p->next=(*head)->next;
(*head)->next=p;

}
fclose(fp);
return0;
}



voidmain()
{
structaddress*L,*head;intn=0;inti=0;FILE*fp=**LL;structaddress*p;

L=(structaddress*)malloc(sizeof(structaddress));
L->next=**LL;
printf("输入ab的值\n");
p=(structaddress*)malloc(sizeof(structaddress));
scanf("%d%d",&p->a,&p->b);
while(p->a!=0)
{
p->next=L->next;
L->next=p;
p=(structaddress*)malloc(sizeof(structaddress));
printf("输入ab的值\n");
scanf("%d%d",&p->a,&p->b);
}

S**eToFile(L,fp);

load(fp,&head);
printf("**读取如下\n");
p=head->next;
while(p!=**LL)
{
printf("%d%d\n",p->a,p->b);
p=p->next;
}
}期待您的答案,我不知道说什么才好,除了谢谢 !

希望以下的回答,能够帮助你。

第1个回答

用户名:RTRT举重肉落货牛RTR43  

关于feof的MSDN,注意括号里面的内容,只日督场础有当试图读取超过文件结尾的**时,才返回非零值:
Thefeoffuncti问答onreturnsanonzerovalue【afterthefirstreadoperationthat选速座attemptstoreadpasttheendofthefile】.Itreturn当s0ifthecurrentpositionisnotendoffile.Thereisnoerrorreturn.
假设文件中有条目1、条目2、条目3
读取完条目1、条目2、条目3后,feof仍然不会返回非零值,只有在你试图读取条目4的时候才返回非零值。

于是这段出派限类意联略跟没语句:
while(!feof(fp))
{
p=(structaddress只*)malloc(size附传宽帮式房of(structaddress));

fread(p,sizeof(struc棉洋taddress),1,fp);
p->next=(*head)->next;
(*head)->next=p;
}
在读取完文件中最后一段**后,还要运行一次
于是最后链表多了一段内容
可以在fread后面再加个判断feof
改成
只吸家界while(!feof(fp))
{
p=(structaddress*)malloc(sizeof(structaddress));

fread(p,sizeof(struc酒事章taddress),1,fp);
if(!feof(f解万硫格滑还确p))
{
p->next适阳油=(*head)准->next;
(*head)->nex升规资t=p;
}
}


警告:用malloc申请的内存在不用的时候要释放,存文件前,你有一个链表;在读取文件时,你把原来的链表彻底抛弃,在抛弃前最好先把链表释放,因为你抛弃后失去对这些链表的控制(因为链表头都被赋予新值了,原来的链表没有用了,但是在程序退出前仍然占用内存空间)。