C 这是一个我的员工工资管理系统的部分代码 麻烦高手帮找错
这是把文件添加到链表里面,我分割出读出数据并添加那段 并且打印出来 ,但是运行直接程序没反应#include<stdio.h>
#include<stdlib.h>
void 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(struct clerk));
readToLink(head);
print(head);
}
void readToLink(struct clerk* head_)
{
//printf("%d\n",sizeof(*info_));
FILE* a;
struct clerk* p=NULL,*pr=NULL;
//printf("%d\n",sizeof(*head_));
head_=(struct clerk*) malloc(sizeof(struct clerk));
//printf("%d\n",sizeof(*head_));
head_->next=NULL;
//printf("%d\n",head_);//测试p
if((a=fopen("jia.txt","a+"))==0)
printf("fail to open the file!\n");
p=head_ ;//保存当前节点的指针//head_是头指针
do
{
pr=(struct clerk*) malloc(sizeof(struct clerk));
fscanf(a,"%10d%10c%2d%10c%6f",&pr->clerkNum,&pr->name,pr->age,&pr->position,&pr->salaries);//按数据块读入文件//按数据块读入文件
//printf("%d\n",sizeof(*p));//测试p
if(p==NULL)
{
printf("No enough memory to allocate!\n");
exit(0);
}
pr=p->next;
p=pr;
p->next=NULL;//尾节点
//pr=pr->next;//让pr指向下一个节点(但是下一个节点是否分配内存?,next指向了该类型 但是要通过malloc函数申请内存,)
//printf("%d\n",pr->age);
}while(!feof(a));
fclose(a);
}
void print(struct clerk* head)
{
while(head->next!=NULL)
{
printf("%10d %10c %2d %10c %6f\n",head->clerkNum,head->name,head->age,head->position,head->salaries);
head=head->next;
}
}