如何从文件中读出并重新建立链表 C++ 10
structbasic*open1(){FILE*fp1;structbasic*head1=NULL;structbasic*tail1=NULL;structbasi...
struct basic *open1(){
FILE *fp1;
struct basic *head1=NULL;
struct basic *tail1=NULL;
struct basic *p1;
fp1=fopen("basic.txt","r");
if(fp1==NULL){
cout<<"cannot open file!"<<endl;
exit(1);
}
else{
for(int i=0;i<N;i++){
fread(p1,sizeof(struct basic),1,fp1);
if(head1==NULL)
head1=p1;
else{
tail1->next=p1;
tail1=p1;
}
}
tail1->next=NULL;
return head1;
fclose(fp1);
}
我不知道我哪里错了,求大神。。还有能帮我看看保存链表到文件里,保存后 发现很多乱码
void save1(basic *head1){
FILE *fp1;
fp1=fopen("basic.txt","w");
if(fp1==NULL){
cout<<"cannot open file!"<<endl;
exit(1);
}
while(head1!=NULL){
fwrite(head1,sizeof(struct basic),1,fp1);
head1=head1->next;
}
fclose(fp1);
} 展开
FILE *fp1;
struct basic *head1=NULL;
struct basic *tail1=NULL;
struct basic *p1;
fp1=fopen("basic.txt","r");
if(fp1==NULL){
cout<<"cannot open file!"<<endl;
exit(1);
}
else{
for(int i=0;i<N;i++){
fread(p1,sizeof(struct basic),1,fp1);
if(head1==NULL)
head1=p1;
else{
tail1->next=p1;
tail1=p1;
}
}
tail1->next=NULL;
return head1;
fclose(fp1);
}
我不知道我哪里错了,求大神。。还有能帮我看看保存链表到文件里,保存后 发现很多乱码
void save1(basic *head1){
FILE *fp1;
fp1=fopen("basic.txt","w");
if(fp1==NULL){
cout<<"cannot open file!"<<endl;
exit(1);
}
while(head1!=NULL){
fwrite(head1,sizeof(struct basic),1,fp1);
head1=head1->next;
}
fclose(fp1);
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询