今日在初学文件录入到链表的时候
通过先打开文件在录入到链表
一开始运行的时候没有问题 但过了一段时间不知道为什么运行时就不会接着跑下去了是因为不小心改到哪里了吗?
#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;
}
问题可能出在第三个函数上
你的代码也不全,只能猜测一下。
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;
}
}