| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1505 人关注过本帖
标题:已经把一个单向链表存入磁盘,现在如何读取呢?即如何从磁盘中读取一个链表 ...
只看楼主 加入收藏
liumoc
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2017-11-28
结帖率:33.33%
收藏
已结贴  问题点数:10 回复次数:3 
已经把一个单向链表存入磁盘,现在如何读取呢?即如何从磁盘中读取一个链表文件
将链表文件写入磁盘的代码:
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Book)

int n=0;
struct Book
    {
        int num;                    //编号
        char name[20];              //书名
        char author[20];            //作者
        char publishing[20];        //出版社
        char type[20];           //类型
        char date[20];            //出版日期
        int salesvolum;           //售价
        int xiaoliang;          //销量
        struct Book *next;
    };


int main()
{
    void save();
    printf("请输入书籍编号,书名,作者,出版社,类型,出版时间,售价,销量...\n");
    save();
    return 0;
}

struct Book *creat()  //创建动态链表函数
{
    struct Book *head;  
    struct Book *p1,*p2;
    n=0;
    p1=p2=(struct Book *)malloc(LEN);
    scanf("%d%s%s%s%s%s%d%d",&p1->num,&p1->name,&p1->author,&p1->publishing,p1->type,&p1->date,&p1->salesvolum,&p1->xiaoliang);     //读入图书的信息
    head=NULL;
    while(p1->num!=0)   //当读入编号为0时就结束读入
    {
        n=n+1;
        if(n==1) head=p1;
        else p2->next=p1;
        p2=p1;
        p1=(struct Book *)malloc(LEN);
        scanf("%d%s%s%s%s%s%d%d",&p1->num,&p1->name,&p1->author,&p1->publishing,p1->type,&p1->date,&p1->salesvolum,&p1->xiaoliang);
    }
    p2->next=NULL;
}

void save()
{
    struct Book *creat();
    FILE *fp;
    struct Book *pt;
    pt=creat();
    if(!(fp=fopen("bookceshi.dat","wb")))
    {
        printf("can not open this file!\n");
        return;
    }
    while(pt!=NULL)
        {
            if(fwrite(pt,sizeof(struct Book),1,fp)!=1)
            printf("file write error!\n");
            pt=pt->next;
        }
    fclose(fp);
}

我读取的代码:
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Book)

int n=0;
struct Book
    {
        int num;                    //编号
        char name[20];              //书名
        char author[20];            //作者
        char publishing[20];        //出版社
        char type[20];           //类型
        char date[20];            //出版日期
        int salesvolum;           //售价
        int xiaoliang;          //销量
        struct Book *next;
    };

int main()
{
    int i;
    struct Book *head;
    struct Book *p1,*p2;
    FILE *fp;
    if(!(fp=fopen("bookceshi.dat","rb")))
    {
        printf("can not open this file!\n");
        exit(0);
    }
    p1=p2=(struct Book*)malloc(LEN);
    head=NULL;

    while(fread(p1,sizeof(struct Book),1,fp)==1)
        {
        n=n+1;
        if(n==1) head=p1;
        else p2->next=p1;
        p2=p1;
        p1=(struct Book *)malloc(LEN);
        }
    p2->next=NULL;
    for(;p1!=NULL;)
        {
            printf("%4d %-10s %-10s %-10s %-10s %-10s %d %d\n",p1->num,p1->name,p1->author,p1->publishing,p1->type,p1->date,p1->salesvolum,p1->xiaoliang);
            p1=p1->next;
        }

    fclose(fp);
    return 0;
}

请大神帮忙看看我的代码,为什么读取出来的文件是乱码

搜索更多相关主题的帖子: 链表 读取 struct int char 
2017-12-26 01:32
liumoc
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2017-11-28
收藏
得分:0 
保存文件的代码是可以运行的,但是读取的代码运行之后会提示程序错误,直接弹出停止运行的窗口!请大神帮我看看,这里完全是自学,老师讲的不是很详细,希望大佬们指点迷津~
2017-12-26 01:35
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10539
专家分:42927
注 册:2014-5-20
收藏
得分:5 
简单改了一下,参考。
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Book)
#define TLEN sizeof(struct TBook)

struct Book
{
    int num;               //编号
    char name[20];         //书名
    char author[20];       //作者
    char publishing[20];   //出版社
    char type[20];         //类型
    char date[20];         //出版日期
    int salesvolum;        //售价
    int xiaoliang;         //销量
};

struct TBook
{
    struct Book sb;
    struct TBook *next;
};

struct TBook *creat();
struct TBook *read(char *path);
int save(struct TBook *h, char *path);
void _list(struct TBook *h);
void _free(struct TBook *h);

int main()
{
    printf("请输入: 书籍编号,书名,作者,出版社,类型,出版时间,售价,销量...\n");
    struct TBook *head = creat();
    _list(head);
    save(head, "bookceshi.dat");
    _free(head);
    head = read("bookceshi.dat");
    _list(head);
    _free(head);
    return 0;
}

struct TBook *creat()  //创建动态链表函数
{
    struct TBook tb, *head=NULL, *p1,*p2;
    if (scanf("%d%s%s%s%s%s%d%d",&tb.sb.num, tb.sb.name, tb.sb.author, tb.sb.publishing,
        tb.sb.type, tb.sb.date, &tb.sb.salesvolum, &tb.sb.xiaoliang)==8 && tb.sb.num!=0)     //读入图书的信息
    {
        head = (struct TBook *)malloc(TLEN);
        *head = tb;
        head->next = NULL;
        p2 = head;
    }
    while (scanf("%d%s%s%s%s%s%d%d",&tb.sb.num, tb.sb.name, tb.sb.author, tb.sb.publishing,
       tb.sb.type, tb.sb.date, &tb.sb.salesvolum, &tb.sb.xiaoliang)==8 && tb.sb.num!=0)   //当读入编号为0时就结束读入
    {
        p1 = (struct TBook *)malloc(TLEN);
        *p1 = tb;
        p1->next = NULL;
        p2->next = p1;
        p2 = p1;
    }
    return head;
}

struct TBook *read(char *path)
{
    FILE *fp;
    if (!(fp=fopen(path,"rb")))
    {
        printf("can not open this file!\n");
        return NULL;
    }
    struct TBook tb, *head=NULL, *p,*p2;
    if (fread(&tb,LEN,1,fp) == 1)
    {
        head = (struct TBook *)malloc(TLEN);
        *head = tb;
        head->next = NULL;
        p2 = head;
    }
    while (fread(&tb,LEN,1,fp) == 1)
    {
        p = (struct TBook *)malloc(TLEN);
        *p = tb;
        p->next = NULL;
        p2->next = p;
        p2 = p;
    }
    fclose(fp);
    return head;
}

int save(struct TBook *h, char *path)
{
    FILE *fp;
    if (!(fp=fopen(path,"wb")))
    {
        printf("can not open this file!\n");
        return 0;
    }
    int ret=1;
    while (h)
    {
        if (fwrite(h,LEN,1,fp)!=1)
        {
            printf("file write error!\n");
            ret = 0;
            break;
        }
        h = h->next;
    }
    fclose(fp);
    return ret;
}

void _list(struct TBook *h)
{
    for (; h; h=h->next)
        printf("%d \t %s \t %s \t %s \t %s \t %s \t %d \t %d\n",
            h->sb.num, h->sb.name, h->sb.author, h->sb.publishing,
            h->sb.type,h->sb.date, h->sb.salesvolum, h->sb.xiaoliang);
}

void _free(struct TBook *h)
{
    struct TBook *p;
    while (h)
    {
        p = h->next;
        free(h);
        h = p;
    }
}

2017-12-26 06:04
liaohs
Rank: 4
等 级:业余侠客
威 望:7
帖 子:61
专家分:292
注 册:2017-11-26
收藏
得分:5 
回复 楼主 liumoc
最后的循环中,p1的初值错了。应该从头开始head
2017-12-30 22:48
快速回复:已经把一个单向链表存入磁盘,现在如何读取呢?即如何从磁盘中读取一个 ...
数据加载中...
 
   



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

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