| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 495 人关注过本帖
标题:求助将链表中数据写入文件,将文件中数据读取并建立链表的问题
只看楼主 加入收藏
nnnn031
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-12-23
收藏
 问题点数:0 回复次数:3 
求助将链表中数据写入文件,将文件中数据读取并建立链表的问题
struct room
{
    int floor;
    int num;
    int type;
    char direction[2];
    int price;
    char ruzhu;
    struct room *next;
};
struct room *writeroom(struct room *head)
{
    FILE *p;
    p=fopen("room","w");
    fwrite(head,sizeof(struct room),1,p);
    do
        {
        head=head->next;
        fwrite(head,sizeof(struct room),1,p);
        }while(head);
    fclose(p);
}
struct room *readroom()
{
    FILE *p;
    struct room *head,*tail,*new1;
    p=fopen("room","r");
    new1=(struct room *)malloc(sizeof(struct room));
    tail=(struct room *)malloc(sizeof(struct room));
    head=new1;
    while(fread(tail,sizeof(struct room),1,p))
    {
        new1->next=tail;
        new1=tail;
        tail=(struct room *)malloc(sizeof(struct room)); }
    new1->next=NULL;
    fclose(p);
    return head;
}
这两个函数有问题吗,我写入读取再输出会有乱码输出
搜索更多相关主题的帖子: direction price 
2014-12-23 16:05
longwu9t
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:732
专家分:2468
注 册:2014-10-9
收藏
得分:0 
能写入能读取只是乱码
是不是没有rewind

Only the Code Tells the Truth             K.I.S.S
2014-12-23 16:44
xufan
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:6
帖 子:232
专家分:804
注 册:2008-10-20
收藏
得分:0 
写入的是二进制方式, 读取出来的肯定是乱码。

~~~~~~我的明天我知道~~~。
2014-12-23 16:50
nnnn031
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-12-23
收藏
得分:0 
回复 3楼 xufan
不全是乱码,链表前面的数据输出是正常的,最后会多出一组乱码数据
2014-12-23 17:12
快速回复:求助将链表中数据写入文件,将文件中数据读取并建立链表的问题
数据加载中...
 
   



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

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