保存读取文件时出错

来源:百度知道 编辑:UC知道 时间:2024/05/03 08:08:13
这个是创建文件
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct info)
struct info
{ int num;
char name[20];
struct info *next;
};
void main()
{ int i;
struct info *head;
struct info *p;
struct info *p1;
FILE *fp;
p=(struct info *)malloc(LEN);
head=p;
for(i=0;i<1;i++)
{p->num=(i+1);
scanf("%s",&p->name);
p1=p;
p=(struct info *)malloc(LEN);
p1->next=p;
}p1->next=NULL;
p=head; if((fp=fopen("bb.dat","wb"))==NULL)printf("error");
while(p!=NULL)
{fwrite(p,LEN,1,fp);p=p->next;}
fclose(fp);
}

这个是追加的
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct info)
struct info
{ int num;
char name[20];
struct info *next;
};
struct info * load()
{ struct

那还是追加程序出错了.

改不对了不要指出我的问题哈哈哈.//

#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct info)
struct info
{ int num;
char name[20];
struct info *next;
};
struct info * load()
{ struct info *head;
struct info *p;
struct info *p1;
FILE *fp;
if((fp=fopen("bb.dat","rb"))==NULL)printf("error"); ///怎么光打印不退出啊?
p=(struct info *)malloc(LEN);
head=NULL; ////////使得头总是空
while(p!=NULL) ////////如果只创建空文件后再追加,皮就是空了,这句不没用了?直接跳到后面....导致头为空...
{fread(p,LEN,1,fp);
if(head==NULL)head=p;

p1=p;p=(struct info *)malloc(LEN);p=p1->next;}
fclose(fp);
return (head); //传了个空的头
}
void main()
{
int i;
struct info *head;
struct info *p;
struct info *p1;
FILE *fp;
head=load();/////承上:为空...
p=head;
while(p!=NULL) ///////所以皮也为空...这段没用直接后面...
{p1=p;p=p