把文件的内容加载到链表,怎么读好,用fread,还是fscanf?
//-----------------------结构体----------------------
struct atm
{
char name[20];//姓名
char pwd[20]; //密码
double money; //余额
atm *next;
}*head;
//-------------------------加载文件内容函数-----------------
void
load()
{
int i,n=0;
open: if((fp=fopen("user.txt","r"))==NULL)//判断文件打开是否成功
{
perror("打开出错");
goto create;
}
struct atm *p1,*p2,*pre;
head=pre=p1=(struct atm*)malloc(sizeof(struct atm));//新建链表头
strcpy(p1->name,"");
strcpy(p1->pwd,"");
p1->money=0;
p1->next=NULL;
while(!feof(fp))//将文件中的数据载入到新的链表
{
p2=creatlink(p1);//调用函数创建新链
//fscanf(fp,"%s %s %lf\n",p2->name,p2->pwd,p2->money);
fread(p2,sizeof(struct atm),1,fp); //将数据读入新链
n++;//获得链表长度
}
fclose(fp);//关闭文件
OutputList(head);//遍历
return;
create: if((fp=fopen("user.txt","w"))==NULL)//判断文件打开是否成功
{
perror("创建出错");
exit(1); //退出
}
}
//-------------------user.txt里德内容----------------------
1 1 1000.000000
2 2 2000.000000
3 3 3000.000000
问题:用fscanf时程序出错,被迫中止,
用fread时打印数据不正确
高手指教啊...