| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1007 人关注过本帖
标题:求助!在把文件数据载入链表时出现了问题!!!
只看楼主 加入收藏
ccmike98
Rank: 2
来 自:苏州
等 级:论坛游民
帖 子:76
专家分:56
注 册:2010-7-13
结帖率:75%
收藏
已结贴  问题点数:20 回复次数:11 
求助!在把文件数据载入链表时出现了问题!!!
#include<stdio.h>
#include<malloc.h>
struct book//图书信息
{
    int b_no;
    char b_name[20];
    char press[20];//出版社
    float price;//单价
    char borrow;//是否借出
    int borrow_no;//借书人编号
    struct book *next;
};

struct book *in_book()//载入图书信息
{   
    char s[1000];
    struct book *p,*last,*head;
    p=(struct book*) malloc(sizeof(struct book));
    FILE *fp;
    if((fp=fopen("D://book.txt","r"))!=NULL)
    {
        
        fgets(s,1000,fp);            //吸收文件第一行表头
        
        if(!feof(fp))
        {
            fscanf(fp,"%d%s%s%f%c",&p->b_no,p->b_name,p->press,&p->price,&p->borrow);
            p->next=NULL;
            last=p;
            head=p;
        
   
            while(!feof( fp ))
            {
                p=(struct book*) malloc(sizeof(struct book));
                fscanf(fp,"%d%s%s%f%c",&p->b_no,p->b_name,p->press,&p->price,&p->borrow);
                p->next=NULL;
                last->next=p;
                last=p;
            }
        }
        printf("载入图书信息成功\n");
        return head;
    }
    else
        printf("载入图书信息失败\n");
    fclose(fp);
}
main()
{
    struct book *p;
    p=in_book();
}
文件没有语法错误,但运行起来错了!!!

求指教!!

我急用啊

book.txt中的东西是:
图书编号      书名             出版社    单价    是否借出   
12345678   C语言程序设计      清华大学    38.00      y
12345679   四级词汇           兴界国      25.00      n
12345680   微积分             高等教育    20.00      n
12345681   杜拉拉升职记       中国科技大  20.00      n
搜索更多相关主题的帖子: 链表 数据 文件 载入 
2010-09-13 21:47
ccmike98
Rank: 2
来 自:苏州
等 级:论坛游民
帖 子:76
专家分:56
注 册:2010-7-13
收藏
得分:0 




问题貌似处在fscanf(fp,"%d%s%s%f%c",&p->b_no,p->b_name,p->press,&p->price,&p->borrow);

中不能用%f,把%f改了就可以了,但我还是要用%f啊!!!

谁来救救我。
2010-09-13 22:47
清风拂晓
Rank: 8Rank: 8
来 自:火星
等 级:蝙蝠侠
威 望:1
帖 子:356
专家分:889
注 册:2010-8-13
收藏
得分:6 
那个改了 没差 还是会出现载入图书信息失败

清风拂暮(木)
2010-09-13 22:50
清风拂晓
Rank: 8Rank: 8
来 自:火星
等 级:蝙蝠侠
威 望:1
帖 子:356
专家分:889
注 册:2010-8-13
收藏
得分:0 
if((fp=fopen("D://book.txt","r"))!=NULL)
这里的问题

清风拂暮(木)
2010-09-13 22:51
清风拂晓
Rank: 8Rank: 8
来 自:火星
等 级:蝙蝠侠
威 望:1
帖 子:356
专家分:889
注 册:2010-8-13
收藏
得分:0 
没哪位高手来回答? 我也想学习下为什么

清风拂暮(木)
2010-09-14 13:16
erikyo
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:2
帖 子:270
专家分:1154
注 册:2010-6-10
收藏
得分:6 
#include<stdio.h>
#include<stdlib.h>

struct book//图书信息
{
    int b_no;
    char b_name[20];
    char press[20];//出版社
    float price;//单价
    char borrow;//是否借出
    int borrow_no;//借书人编号
    struct book *next;
};

struct book *in_book()//载入图书信息
{   
    char s[1000];
    struct book *p=NULL,*last=NULL,*head=NULL;
    FILE *fp;


    if((fp=fopen("F://book.txt","r"))==NULL)
    {
        printf("can'r open file!\n");
        return NULL;
    }
        
    fgets(s,1000,fp);            //吸收文件第一行表
   
    while(fgets(s,1000,fp)!=NULL)
    {
        p=(struct book*) malloc(sizeof(struct book));
        if(NULL ==  p)
        {
            printf("In inbook, NULL == p!\n");
            return NULL;
        }

        sscanf(s,"%d %s %s %f %c",&p->b_no,p->b_name,p->press,&p->price,&p->borrow);
        p->next= NULL;
        if(NULL == head)
            head = p;
        else
            last->next = p;
        last = p;
    }
        
    printf("载入图书信息成功\n");

    return head;
}

void Print_list(struct book* head)
{
    struct book *pnext = head;

    while(pnext)
    {
        printf("%d %s %s %.2f %c\n",pnext->b_no,pnext->b_name,pnext->press,pnext->price,pnext->borrow);
        pnext = pnext->next;
    }
}

int main()
{
    struct book *p = NULL;
    p=in_book();
    Print_list(p);

    return 0;
}
2010-09-14 16:04
清风拂晓
Rank: 8Rank: 8
来 自:火星
等 级:蝙蝠侠
威 望:1
帖 子:356
专家分:889
注 册:2010-8-13
收藏
得分:0 
既然已经p=(struct book*) malloc(sizeof(struct book));
        if(NULL ==  p)
为什么还要
if(NULL ==  p)
        {
            printf("In inbook, NULL == p!\n");
            return NULL;
?这部分是干什么用的 ?P不是指向了一个空的空间了?、

清风拂暮(木)
2010-09-14 17:15
守候
该用户已被删除
收藏
得分:6 
提示: 作者被禁止或删除 内容自动屏蔽
2010-09-14 17:25
清风拂晓
Rank: 8Rank: 8
来 自:火星
等 级:蝙蝠侠
威 望:1
帖 子:356
专家分:889
注 册:2010-8-13
收藏
得分:0 
还有sscanf(s,"%d %s %s %f %c",&p->b_no,p->b_name,p->press,&p->price,&p->borrow);
这里应该是fscanf(fp,"%d %s %s %f %c",&p->b_no,p->b_name,p->press,&p->price,&p->borrow);

清风拂暮(木)
2010-09-14 17:27
清风拂晓
Rank: 8Rank: 8
来 自:火星
等 级:蝙蝠侠
威 望:1
帖 子:356
专家分:889
注 册:2010-8-13
收藏
得分:0 
有什么原因引起malloc失败?

清风拂暮(木)
2010-09-14 17:32
快速回复:求助!在把文件数据载入链表时出现了问题!!!
数据加载中...
 
   



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

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