没办法了,真的没办法了,我哪里错了,告诉我,我一定改。
这是逻辑错误,员工工资管理系统,我卡在把文件读取出来放入链表里面这一步,我把这一步代码切出来,单独弄一个测试程序,这是最后修改的,中间有很多测试代码,大神可以复制去运行一下分析一下,将感激不尽,情况就是运行就退出,我想知道我到底错在哪。高手们#include<stdio.h>
#include<stdlib.h>
struct clerk* readToLink(struct clerk* head_);
void print(struct clerk* head);
struct clerk
{
char clerkNum[10];
char name[20];
int age;
char position[10];
char salaries[10];
struct clerk* next;
};
main()
{
struct clerk*head=NULL;
//printf("%d\n",sizeof(*head));
head=readToLink(head);
print(head);
}
struct clerk* readToLink(struct clerk* head_)
{
FILE* a;
struct clerk* p=head_,*pr=NULL;
//printf("%d\n",sizeof(*head_));//测试
//head_=(struct clerk*)malloc(sizeof(struct clerk));//测试
//printf("%d\n",sizeof(*head_));//测试
//printf("%d\n",head_);//测试
if((a=fopen("jia.txt","a+"))==0)
printf("fail to open the file!\n");
do
{
pr=(struct clerk*)malloc(sizeof(struct clerk));
if(pr==NULL)
{
printf("No enough memory to allocate!\n");
exit(0);
}
fscanf(a,"%10d%5s%2d%10s%6f",&(pr->clerkNum),&(pr->name),&(pr->age),&(pr->position),&(pr->salaries));//按数据块读入文件//按数据块读入文件
//printf("%d\n",sizeof(*p));//测试p
p->next=pr;
p=pr;
pr=pr->next;//让pr指向下一个节点(但是下一个节点是否分配内存?,next指向了该类型 但是要通过malloc函数申请内存,)
}while(!feof(a));
pr->next=NULL;
return head_;
fclose(a);
}
void print(struct clerk* head)
{
while(head->next!=NULL)
{
printf("%10d %5s %2d %10s %6f\n",head->clerkNum,head->name,head->age,head->position,head->salaries);
head=head->next;
}
}