| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3822 人关注过本帖
标题:如何利用fwrite将链表中的数据写入到一个文件中?
只看楼主 加入收藏
brisk
Rank: 1
等 级:新手上路
帖 子:29
专家分:8
注 册:2011-3-26
结帖率:66.67%
收藏
 问题点数:0 回复次数:1 
如何利用fwrite将链表中的数据写入到一个文件中?
下面是我的源代码,但是却不能正确地读出与写入,能够编译运行,主要看output_file函数和read_file函数
#include "stdio.h"
#include "stdlib.h"
struct node
{int num;
char name[10];
char phone[12];
struct node *next;
};
typedef struct node LINK;
#define MAL (LINK *)malloc(sizeof(LINK))
LINK *creat_link()
{LINK *head,*s,*r;
r=head=MAL;
s=MAL;
scanf("%d%s%s",&s->num,s->name,s->phone);
while(1)
{r->next=s;
r=s;
s=MAL;
scanf("%d",&s->num);
if(s->num==-1)
break;
else
scanf("%s%s",s->name,s->phone);
}
r->next=NULL;
return head;
}
void print_link(LINK *head)
{if(head->next==NULL)
printf("LINK is NULL!");
else
for(head=head->next;head!=NULL;head=head->next)
printf("No.%d:%s %s\n",head->num,head->name,head->phone);
}
void output_file(LINK *head)//写出数据
{FILE *open;
if((open=fopen("C:\\a.txt","w+"))==NULL)
{printf("加载失败!");
getchar();
exit(1);
}
for(head=head->next;head!=0;head=head->next)
fwrite(head,sizeof(head),1,open);
fclose(open);
}
LINK *read_file()//读入数据
{LINK *head,*s,*r;
FILE *open;
if((open=fopen("C:\\a.txt","r"))==NULL)
{printf("加载失败!");
setbuf(stdin,NULL);
getchar();
exit(1);
}
head=r=MAL;
while(!feof(open))
{s=MAL;
fread(s,sizeof(s),1,open);
r->next=s;
r=s;
}
fclose(open);
return head;
}
void main()
{LINK *head;
head=creat_link();
print_link(head);
output_file(head);
head=read_file();
print_link(head);
}
请各位高手看下
搜索更多相关主题的帖子: include 源代码 如何 
2011-07-17 22:11
stophin
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:227
专家分:618
注 册:2010-3-26
收藏
得分:0 
程序代码:
#include <conio.h>
#include "stdio.h"
#include "stdlib.h"
#include <iostream>//改动:cout库函数
using namespace std;

struct node
{
       int num;
       char name[10];
       char phone[12];
       struct node *next;
};
typedef struct node LINK;

#define MAL (LINK *)malloc(sizeof(LINK))

LINK *creat_link()
{
     LINK *head,*s,*r;
     r=head=MAL;
     s=MAL;
     cout<<"请依次输入num, name,phone,输入num时输入-1停止输入:"<<endl; //改动:提示性语句
     scanf("%d%s%s",&s->num,s->name,s->phone);
     while(1)
     {
             r->next=s;
             r=s;
             s=MAL;
             scanf("%d",&s->num);
             if(s->num==-1)
                break;
             else
                 scanf("%s%s",s->name,s->phone);
     }
     r->next=NULL;
     return head;
}

void print_link(LINK *head)
{
     if(head->next==NULL)
         printf("LINK is NULL!");
     else
         for(head=head->next;head!=NULL;head=head->next)
            printf("No.%d:%s %s\n",head->num,head->name,head->phone);
}

void output_file(LINK *head)//写出数据
{
     FILE *open;
     if((open=fopen("a.txt","w+"))==NULL)//改动:路径
     {
         printf("加载失败!");
         getchar();
         exit(1);
     }
     for(head=head->next;head!=NULL;head=head->next)
     {
        fwrite(head,sizeof(LINK),1,open);//改动:LINK
     }
     fclose(open);
}

LINK *read_file()//读入数据
{   
     LINK *head,*s,*r,*t;//改动:添加*t,作为标记
     FILE *open;
     if((open=fopen("a.txt","r"))==NULL)//改动:路径
     {
        printf("加载失败!");
        setbuf(stdin,NULL);
        getchar();
        exit(1);
     }
     head=r=MAL;
     while(!feof(open))
     {
        s=MAL;
        fread(s,sizeof(LINK),1,open);//改动:LINK
        r->next=s;
        t=r;//改动,添加*t ,作用在这儿
        r=s;
     }
     t->next=NULL;//改动,添加*t
     fclose(open);
     return head;
}

int main()
{
    LINK *head;
    head=creat_link();
    cout<<"输入的数据:"<<endl; //加入了提示性语句,改动较大,但函数顺序没有改变
    print_link(head);
    cout<<"写入文件..."<<endl;
    output_file(head);
    cout<<"OK!"<<endl<<"读取文件..."<<endl;
    head=read_file();
    cout<<"OK!"<<endl<<"读取的文件:"<<endl;
    print_link(head);
    cout<<"任意键继续..."<<endl;
    getch();//改动:任意键
    return 0;
}
帮你改好了,可以搜索“改动”查看改动的地方,主要是sizeof(LINK)和*t的作用(使末尾添加NULL),楼主可以看一下。
2011-08-12 21:34
快速回复:如何利用fwrite将链表中的数据写入到一个文件中?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016427 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved