红逝167 2021-12-02 15:12 采纳率: 50%
浏览 30
已结题

c文件读入到链表的过程中 编译通过却一直在运行

今日在初学文件录入到链表的时候
通过先打开文件在录入到链表
一开始运行的时候没有问题 但过了一段时间不知道为什么运行时就不会接着跑下去了是因为不小心改到哪里了吗?





#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<malloc.h>
#include<ctype.h>
#include<string.h>
//函数原型
void showall(struct bookinfo*head);  //显示当前的图书信息 
void menu();         //打印菜单 
FILE* openfile(char *name);//用于函数前期打开文件
struct bookinfo* readfile(FILE*);
void search(struct bookinfo*head);//用于寻找某一数据
void addbook(struct bookinfo*head);//用于增加某一本书的信息    返回值待确定?????? 
void delbook(struct bookinfo*head);//用于删除某一本书的信息
void editbook(struct bookinfo*head);//用于对某一本书的信息作修改 
 
int main()
{  
   struct bookinfo *p;
   FILE* infile;
   int input=0;
   char a[20];
   gets(a);     //书籍信息.txt 
   infile=openfile(a);
   p=readfile(infile); 
}

#include"addressBook.h"                    
FILE *openfile(char* name)  
{
    int flag=1;
    char b;
    FILE*infile;                              
    while(flag)
     {
        flag=0; 
        if((infile=fopen(name,"r"))==NULL)
        {
         printf("打开失败\n 请重新输入\n");
         printf("是否愿意重新输入图书管理名 y or n");                   
         b=getchar();
         if(b=='n')
         exit(1);
         else
         scanf("%s",name); 
         flag=1; 
        }
    }  
    printf("打开成功\n"); 
    return (infile);
} 

struct bookinfo*readfile(FILE*infile)
{
    struct bookinfo *p,*head;
    head=NULL;
    p=(struct bookinfo*)malloc(len);
    while(fscanf(infile,"%s%s%s%s%f%d",p->number,p->name,p->author,p->press,&p->price,&p->stocknum)!=EOF)    //字符型不用取地址 
        {
            p->next=head;
            head=p;
            p=(struct bookinfo*)malloc(len);
        }
    free(p); 
    printf("录入完成"); 
    return head;
 } 

问题可能出在第三个函数上

  • 写回答

1条回答 默认 最新

  • 关注

    你的代码也不全,只能猜测一下。
    addbook和delbook这两个函数,你增删了节点,没有返回新的链表头节点,可能会导致错误。比如,当新增的节点插入到了头结点的时候,原来的头结点就已经变了,addbook函数无法返回这个新的节点(你用的一级指针,无法用参数把新的头结点传出来)。delbook函数同理
    你的读取函数修改成下面的试试:

    
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    struct bookinfo
    {
        char number[20];
        char name[40];
        char author[20];
        char press[40];
        float price;
        int stocknum;
        struct bookinfo* next;
    };
    
    
    //函数原型
    void showall(struct bookinfo*head);  //显示当前的图书信息 
    void menu();         //打印菜单 
    FILE* openfile(char *name,FILE** p);//用于函数前期打开文件
    struct bookinfo* readfile(FILE*);
    void search(struct bookinfo*head);//用于寻找某一数据
    void addbook(struct bookinfo*head);//用于增加某一本书的信息    返回值待确定?????? 
    void delbook(struct bookinfo*head);//用于删除某一本书的信息
    void editbook(struct bookinfo*head);//用于对某一本书的信息作修改 
    int main()
    {  
        struct bookinfo *p=0,*t;
        FILE* infile=0;
        int input=0;
        char a[20];
        gets(a);     //书籍信息.txt 
        infile=openfile(a,&infile);
        p=readfile(infile); 
    
        //显示所有书籍
        showall(p);
    
        //这里要释放空间
        while(p)
        {
            t = p->next;
            free(p);
            p = t;
        }
        return 0;
    }
                    
    FILE *openfile(char* name,FILE** p)  
    {
        int flag=1;
        char b;
                                     
        while(flag)
        {
            flag=0; 
            if((*p=fopen(name,"r"))==NULL)
            {
                printf("打开失败\n 请重新输入\n");
                printf("是否愿意重新输入图书管理名 y or n");                   
                b=getchar();
                if(b=='n')
                    exit(1);
                else
                    scanf("%s",name); 
                flag=1; 
            }
        }  
        printf("打开成功\n"); 
        return (*p);
    } 
    struct bookinfo* readfile(FILE*infile)
    {
        struct bookinfo *p,*head;
        int len = sizeof(struct bookinfo);
        head=NULL;
        p=(struct bookinfo*)malloc(len);
        while(!feof(infile))    //字符型不用取地址 
        {
            fscanf(infile,"%s%s%s%s%f%d\n",p->number,p->name,p->author,p->press,&p->price,&p->stocknum);
            //判断读取是否正确
            if(p->press < 0 || p->stocknum < 0)
                break;
            p->next=head;
            head=p;
            p=(struct bookinfo*)malloc(len);
        }
        free(p); 
        fclose(infile);
        printf("录入完成"); 
        return head;
    } 
    //显示当前的图书信息 
    void showall(struct bookinfo*head)  
    {
        struct bookinfo* t = head;
        while(t)
        {
            printf("%s %s %s %s %f %d\n",t->number,t->name,t->author,t->press,t->price,t->stocknum);
            t = t->next;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 12月10日
  • 已采纳回答 12月2日
  • 创建了问题 12月2日

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法