
已解决问题
谷歌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;
}
}期待您的答案,我不知道说什么才好,除了谢谢
!