编程时遇到一个问题。一天了也没有解决。能帮助的感激不尽
我是要解决一个不定数目的数据处理问题。在解决数据输入时没有问题,在把输入的数据写入文件时也没有问题,就是在把文件链接成一个链表后,我想看一些链接是否成功,就调用了一下链接后链表,想输出开始输入的数据,就出了问题了。这是我的程序:其中有很多输出“right"的语句是用来调试哪一步出错的。求高人指点一下。我这都快被整得崩溃了。#include<stdio.h>
#include<stdlib.h>
void input() 输入函数
{
struct employees
{
int number;
char name[20];
char sex;
char danwei[50];
char address[50];
int phone;
float jiben;
float jintie;
float butie;
float yingfa;
float dianhuafei;
float shuidianfei;
float fangzu;
float suodeshui;
float weishengfei;
float gongjijin;
float hejikoukuan;
float shifagongzi;
};
{
struct employees teacher;
char numstr[20],ch;
float x;
FILE *fp;
if((fp=fopen("teacher.dat","wb"))==NULL)
{printf("can't open the file teacher.dat\n");
exit(1);
}
do
{ printf("number:");gets(numstr);teacher.number=atoi(numstr);
printf("name:");gets(teacher.name);
printf("sex:");teacher.sex=getchar();getchar();
printf("danwei:");gets(teacher.danwei);
printf("address:");gets(teacher.address);
printf("phone:");gets(numstr);teacher.phone=atoi(numstr);
printf("jiben:");gets(numstr);teacher.jiben=atof(numstr);
printf("butie:");gets(numstr);teacher.butie=atof(numstr);
printf("yingfa:");gets(numstr);teacher.yingfa=atof(numstr);
printf("dianhuafei:");gets(numstr);teacher.dianhuafei=atof(numstr);
printf("shuidianfei:");gets(numstr);teacher.shuidianfei=atof(numstr);
printf("fangzu:");gets(numstr);teacher.fangzu=atof(numstr);
printf("suodeshui:");gets(numstr);teacher.suodeshui=atof(numstr);
printf("weishengfei:");gets(numstr);teacher.weishengfei=atof(numstr);
printf("gongjijin:");gets(numstr);teacher.gongjijin=atof(numstr);
printf("hejikoukuan:");gets(numstr);teacher.hejikoukuan=atof(numstr);
printf("shifagongzi:");gets(numstr);teacher.shifagongzi=atof(numstr);
fwrite(&teacher,sizeof(struct employees),1,fp);
printf("have another employ record(y/n)?");
ch=getchar();getchar();
}while(ch=='y'||ch=='Y');
fclose(fp);
printf("right1");
}
} 输入函数结束
struct employees2 链接用的函数
{
int number;
char name[20];
char sex;
char danwei[50];
char address[50];
int phone;
float jiben;
float jintie;
float butie;
float yingfa;
float dianhuafei;
float shuidianfei;
float fangzu;
float suodeshui;
float weishengfei;
float gongjijin;
float hejikoukuan;
float shifagongzi;
struct employees2 *link;
};
struct employees2 *load()
{
struct employees2 *head,*p,*q;
FILE *fp;
if((fp=fopen("teacher.dat","rb"))==NULL)
{printf("can't open file \n");
getchar();
exit(1);
}
head=NULL;
q=(struct employees2 *)malloc(sizeof(struct employees2));
while(fread(q,sizeof(struct employees2)-4,1,fp)==1)
{q->link=NULL;
if(head==NULL)
{head==q;
p=head;printf("right2");
}
else
{
p->link=q;
p=p->link;printf("right3");
}
q=(struct employees2 *)malloc(sizeof(struct employees2));
}
free(q);
fclose(fp);
printf("%d",head->phone);printf("right4");
return(head);
} 链接用的函数结束
main() 主函数
{
void input();
int x;
struct employees2 *load();
struct employees2 *pp;
input(); 调用输入函数
printf("right5");
pp=load(); 调用链接函数,大致就是这里出现了问题
printf("right6");
x=pp->phone; 没法执行这一步。一旦执行就显示可执行文件一停止工作
printf("right7");
printf("%d",x);
system("pause");
}