将链表写入文件失败Debug Assertion Failed
只写了一部分但其他的都能运行唯独保存入文件的时候就失败并且弹出提示框,内容如下:Debug Assertion Failed!
Program;C:\Users\等烟归\Desktop\C语言\作业\Debug\luru.exe
File:fprintf.c
Line:56
Expression:str!=NULL
For information on how your program can cause an assertion
failure,see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
代码分了几个部分如下:
chengyuan.h:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define LEN sizeof(struct Chengyuan)/*将结构体长度赋值给LEN*/
struct Chengyuan
{
int num;
char name[20];
char xingbie[6];
char bumen[20];
int zhiling;
int shijian;
int gongzi;
char zhicheng[30];
struct Chengyuan *next;
};
typedef struct Chengyuan CY;
/*函数声明*/
CY *luru(CY *head);
CY *tianjia(CY *head);
void baocun(CY *head);
caidan.c:
#include"chengyuan.h"
extern int n;
int main()
{
CY *head=NULL;
char a;
printf("欢迎使用程序,程序功能:\n");
do
{
printf("1、新员工信息录入\n");
printf("2、员工信息显示\n");
printf("3、员工信息删除\n");
printf("4、搜索员工信息\n");
printf("5、信息文件保存\n");
printf("6、信息文件读取\n");
printf("7、退出\n");
printf("请通过输入数字选择你需要的功能:\n");
scanf(" %c",&a);/*%c前面加一个空格刷新缓冲区*/
switch(a)
{
case'1':head=luru(head);
break;
case'2':break;
case'3':break;
case'4':break;
case'5':baocun(head);
break;
case'6':break;
case'7':exit(0);
break;
default:printf("请输入正确数字\n");
break;
}
}
while(1);
return 0;
}
luru.c:
#include"chengyuan.h"
int n=0;
/*录入员工信息*/
CY * luru(CY *head)
{
int i=0;
char c;
do
{
head=tianjia(head);/*为链表添加新节点,将员工信息录入*/
printf("是否继续添加信息?(Y/N):\n");
scanf(" %c",&c);/*%c前面加一个空格刷新缓冲区*/
i++;
n++;
}
while(c=='Y'||c=='y');
printf("共%d个员工信息成功添加\n",i);
return head;
}
/*添加新节点,输入员工信息*/
CY * tianjia(CY *head)
{
CY *pr=head;
CY *p1;
p1=(CY*)malloc(LEN);/*为新节点创建内存*/
if(p1==NULL)/*若内存不够,输出失败信息并退出程序*/
{
printf("内存不够\n");
exit(0);
}
if(head==NULL)/*若原链表为空,则新建立节点为首节点*/
head=p1;
else/*若原链表不为空,则新建立节点添加到链表尾*/
{
while(pr->next!=NULL)/*将指针移动到表尾*/
{
pr=pr->next;
}
pr->next=p1;/*将新建立节点添加到表尾*/
}
/*输入要添加的员工信息*/
printf("请输入编号:");
scanf("%d",&p1->num);
printf("请输入名字:");
scanf("%s",p1->name);
printf("性别为男(man)/女(woman):");
do
{
scanf("%s",p1->xingbie);
if(strcmp(p1->xingbie,"man")!=0&&strcmp(p1->xingbie,"woman")!=0)
printf("输入错误,请重新输入:");
}
while(strcmp(p1->xingbie,"man")!=0&&strcmp(p1->xingbie,"woman")!=0);
printf("请输入所在部门:");
scanf("%s",p1->bumen);
printf("请输入职工年龄:");
scanf("%d",&p1->zhiling);
printf("请输入工作时间(年):");
scanf("%d",&p1->shijian);
printf("请输入工资(元):");
scanf("%d",&p1->gongzi);
printf("请输入职称:");
scanf("%s",p1->zhicheng);
p1->next=NULL;/*将新节点设置为表尾*/
return head;
}
tianjia.c:
#include"chengyuan.h"
int n=0;
/*录入员工信息*/
CY * luru(CY *head)
{
int i=0;
char c;
do
{
head=tianjia(head);/*为链表添加新节点,将员工信息录入*/
printf("是否继续添加信息?(Y/N):\n");
scanf(" %c",&c);/*%c前面加一个空格刷新缓冲区*/
i++;
n++;
}
while(c=='Y'||c=='y');
printf("共%d个员工信息成功添加\n",i);
return head;
}
/*添加新节点,输入员工信息*/
CY * tianjia(CY *head)
{
CY *pr=head;
CY *p1;
p1=(CY*)malloc(LEN);/*为新节点创建内存*/
if(p1==NULL)/*若内存不够,输出失败信息并退出程序*/
{
printf("内存不够\n");
exit(0);
}
if(head==NULL)/*若原链表为空,则新建立节点为首节点*/
head=p1;
else/*若原链表不为空,则新建立节点添加到链表尾*/
{
while(pr->next!=NULL)/*将指针移动到表尾*/
{
pr=pr->next;
}
pr->next=p1;/*将新建立节点添加到表尾*/
}
/*输入要添加的员工信息*/
printf("请输入编号:");
scanf("%d",&p1->num);
printf("请输入名字:");
scanf("%s",p1->name);
printf("性别为男(man)/女(woman):");
do
{
scanf("%s",p1->xingbie);
if(strcmp(p1->xingbie,"man")!=0&&strcmp(p1->xingbie,"woman")!=0)
printf("输入错误,请重新输入:");
}
while(strcmp(p1->xingbie,"man")!=0&&strcmp(p1->xingbie,"woman")!=0);
printf("请输入所在部门:");
scanf("%s",p1->bumen);
printf("请输入职工年龄:");
scanf("%d",&p1->zhiling);
printf("请输入工作时间(年):");
scanf("%d",&p1->shijian);
printf("请输入工资(元):");
scanf("%d",&p1->gongzi);
printf("请输入职称:");
scanf("%s",p1->zhicheng);
p1->next=NULL;/*将新节点设置为表尾*/
return head;
}
baocun.c:
#include"chengyuan.h"
extern int n;
void baocun(CY *head)
{
CY *p1=head;
CY *p2;
FILE *fp;
if(head==NULL)
{
printf("无信息可保存。\n");
return;
}
if(fp=fopen("chengyuan.dat","wb")==NULL)
{
printf("内存不够。\n");
return;
}
while(p1!=NULL)
{
fprintf(fp,"%d",p1->num);
p1=p1->next;
}
fclose(fp);
return;
}
求大神指点迷津
[此贴子已经被作者于2018-3-2 17:11编辑过]