以下是本人编写的程序,在文件读取这快总是编不成功,哪位C语言高手帮帮我啊,小弟在此不甚感激!!
#include "stdlib.h"
#include "stdio.h"
struct salary
{ char name[20];
long num ;
int age ;
char sex ;
float jbgz,sj,kk,sjgz;
struct salary*next ;
} *head=NULL, *this, *new;
void new_record(void)
{ char numstr[20];
new=(struct salary*) malloc( sizeof(struct salary) );
if(head==NULL) head=new ;
else
{ this=head ;
while(this->next!=NULL) this=this->next ;
this->next=new ;
}
this=new ;
printf("\nenter name:");
gets(this->name);
printf("enter number:");
gets(numstr); this->num=atoi(numstr);
printf("enter age:");
gets(numstr); this->age=atoi(numstr);
printf("enter sex:");
gets(numstr); this->sex=numstr[0];
printf("enter jbgz:");
gets(numstr); this->jbgz=atof(numstr);
printf("enter sj:");
gets(numstr); this->sj=atof(numstr);
printf("enter kk:");
gets(numstr); this->kk=atof(numstr);
this->sjgz=(this->jbgz)-(this->sj)-(this->kk);
this->next=NULL ;
}
void search_record(void)
{ char n[20];
int i=0;
printf("enter the name: ");
gets(n);
this=head;
while(strcmp(this->name,n)&&this!=NULL)
this=this->next;
printf("%8s%16s%8s%8s%8s%16s\n","record","name","num","age","sex","sjgz");
printf("%8d",++i);
printf("%16s",this->name);
printf("%8ld",this->num);
printf("%8d",this->age);
printf("%8c",this->sex);
printf("%16.2f\n",this->sjgz);
}
void average(void)
{float sum,ave;
int i;
sum=0;
if(head==NULL)
{ printf("empty list\n"); }
else
{
this=head;
for(i=1;this->next!=NULL;i++)
{ sum+=this->sjgz;
this=this->next;
}
ave=sum/i;
printf("the workers average sjgz:%f\n",ave);
}
}
void listall(void)
{ int i=0 ;
if(head==NULL) { printf("enmpty list.\n"); return ; }
this=head ;
printf("%8s%16s%8s%8s%8s%16s\n","record","name","num","age","sex","sjgz");
do
{ printf("%8d",++i);
printf("%16s",this->name);
printf("%8ld",this->num);
printf("%8d",this->age);
printf("%8c",this->sex);
printf("%16.2f\n",this->sjgz);
this=this->next ;
} while(this!=NULL);
}
void p_record(void)
{int i=0;
float t;
if(head=NULL)
{ printf("empty list\n");
return;}
else
{
for(;head->next!=NULL;head++)
{
this=head+1;
for(;this!=NULL;this++)
{
if(head->sjgz<this->sjgz)
{ t=head->sjgz;
head->sjgz=this->sjgz;
this->sjgz=t;}
this=this->next;}
}
}
this=head;
printf("%8s%16s%8s%8s%8s%16s\n","record","name","num","age","sex","sjgz");
do
{ printf("%8d",++i);
printf("%16s",this->name);
printf("%8ld",this->num);
printf("%8d",this->age);
printf("%8c",this->sex);
printf("%16.2f\n",this->sjgz);
this=this->next ;
} while(this!=NULL);
}
void keep_file(void)
{ FILE *fp;
char filename[20];
printf("enter file name\n");
gets(filename);
if( (fp=fopen(filename,"wb"))==NULL)
{ printf(" empty list\n" );
return;}
this=head;
while(this!=NULL)
{ fwrite(this,sizeof(struct salary),1,fp);
this=this->next;}
fclose(fp);
printf("save success\n");
}
struct salary *open_file()
{ FILE *fp;
char filename[20];
struct salary *p,*q,*h=NULL;
printf(" enter file name\n");
gets(filename);
fp=fopen(filename,"rb");
p=(struct salary *)malloc(sizeof(struct salary));
p=h;
while(!feof(fp))
{
fread(this,sizeof(struct salary),1,fp);
p->next=(struct salary *)malloc(sizeof(struct salary));
q=p;p=p->next; }
q->next=NULL;
fclose(fp);
printf("open success\n");
return h;
}
main()
{
char ch ; int flag=1 ;
while(flag)
{ printf("--------------------------\n");
printf("| O) open file |\n");
printf("| K) keep to file\n");
printf("| E) enter new record |\n");
printf("| S) search a record |\n");
printf("| L) list all record |\n");
printf("| p) get all record ||\n");
printf("| A) view the average salary |\n");
printf("| Q) QUIT |\n");
printf("-------------------------\n");
printf("select command ?"); ch=getch(); printf("%c\n\n",ch);
switch(ch)
{ case 'e' :
case 'E' : new_record(); break ;
case 's' :
case 'S' : search_record();break;
case 'a' :
case 'A' : average();break;
case 'l' :
case 'L' : listall(); break;
case 'P' :
case 'p' : p_record();break;
case 'o' :
case 'O' : head=open_file();break;
case 'k' :
case 'K' : keep_file();break;
case 'q' :
case 'Q' : flag=0 ; break ;
default : break ;
}
}
}