int open_file(stu_list &s_list,char *FileName)//打开文件FileName将信息读入链表
{
FILE *fp_r;
char buf_temp[40];
int i=0;
link i_temp;
memset(buf_temp,0,sizeof(buf_temp));
if((fp_r=fopen(FileName,"r"))==NULL)
{
o_str("打开文件失败");
getch();
return -1;
}
fgets(buf_temp,40,fp_r);//读取学生个数
memset(buf_temp,0,sizeof(buf_temp));
while((fgets(buf_temp,40,fp_r))!=NULL)//读取学生信息
{
i_temp=(link)malloc(sizeof(struct student));
if(i_temp==NULL)
{
o_str("动态分配内存失败!");
getch();
return -1;
}
strcpy(i_temp->stu_num,strtok(buf_temp,"|"));
strcpy(i_temp->stu_name,strtok(NULL,"|"));
i_temp->stu_grade=(float)atof(strtok(NULL,"|"));
i_temp->pagenum=i/8+1;
memset(buf_temp,0,sizeof(buf_temp));
insert_s(s_list,i_temp);
i++;
}
if(i==0)
{
o_str("文件中无学生记录,按任意键继续...");
getch();
return -1;
}
fclose(fp_r);
return 0;
}
int save_stu(stu_list s_list,char *FileName)//保存输入的学生信息到文件FileName
{
FILE *fp_w;
link p;
p=s_list.head;
p=p->next;
if(p==NULL)
{
o_str("无学生数据,按任意键继续...");
if((fp_w=fopen(FileName,"w"))==NULL)
printf("\n\t\t\t打开文件 %s 错误!",FileName);
fprintf(fp_w,"学生总数:%d|\n",s_list.len);//保存学生数
fclose(fp_w);
getch();
return 0;
}
o_str("正在保存...");
i_sleep(1);
if((fp_w=fopen(FileName,"w"))==NULL)
printf("\n\t\t\t打开文件 %s 错误!",FileName);
else
{
fprintf(fp_w,"学生总数:%d|\n",s_list.len);//保存学生数
while(p->next!=NULL)//保存学生信息
{
fprintf(fp_w,"%s|%s|%5.2f|\n",p->stu_num,p->stu_name,p->stu_grade);
p=p->next;
}
fprintf(fp_w,"%s|%s|%5.2f|\n",p->stu_num,p->stu_name,p->stu_grade);
fclose(fp_w);
o_str("保存成功!");
}
i_sleep(1);
return 0;
}
int add_stu(char *FileName)//添加单个学生记录到文件FileName
{
stu_list s_list;
init_list(s_list);
system("cls");
open_file(s_list,FileName);
system("cls");
while(1)
{
get_stu(s_list);
printf("\n\t\t是否继续添加Y/N?:");
if((getchar()=='Y')||(getchar()=='y'))
continue;
else
break;
}
printf("\n\t\t是否保存修改Y/N?:");
if((getchar()=='Y')||(getchar()=='y'))
{
save_stu(s_list,FileName);
}
free_list(s_list);
return 0;
}
int update_stu(char *FileName)//修改学生记录
{
stu_list s_list,list_temp;
char num[15];
char name_new[15];
float grade_new;
init_list(s_list);
init_list(list_temp);
system("cls");
if(open_file(s_list,FileName)<0)
{
free_list(s_list);
free_list(list_temp);
return -1;
}
system("cls");
link p,s,q;
p=s_list.head;
p=p->next;
q=s_list.head->next;
o_str("请输入学生学号:");
scanf("%15s",&num);
while(p)//查找学号为num的学生
{
if(strcmp(num,p->stu_num)==0)
{
s=(link)malloc(sizeof(struct student));
if(s==NULL)
{
o_str("动态分配内存失败!");
return -1;
}
strcpy(s->stu_num,p->stu_num);
strcpy(s->stu_name,p->stu_name);
s->stu_grade=p->stu_grade;
insert_s(list_temp,s);
}
p=p->next;
}
if(list_temp.len==0)
{
o_str("没有符合条件的学生记录,按任意键返回...");
getch();
return -1;
}
if(list_temp.len>1)
{
o_str("学号不唯一,错误,按任意键返回...");
getch();
return -1;
}
if(print_stu(list_temp)<0)
{
free_list(list_temp);
o_str("按任意键返回....");
getch();
return -1;
}
o_str("请输入学生姓名:");
scanf("%15s",&name_new);
o_str("请输入学生成绩:");
scanf("%f",&grade_new);
while(q)
{
if(strcmp(num,q->stu_num)==0)
break;
q=q->next;
}
memset(q->stu_name,0,sizeof(q->stu_name));
strcpy(q->stu_name,name_new);
q->stu_grade=grade_new;
o_str("确认保存修改吗?N/Y:");
if(getchar()=='Y'||getchar()=='y')
{
save_stu(s_list,FileName);
free_list(list_temp);
free_list(s_list);
}
else
{
o_str("您没有做任何修改!按任意键继续...");
free_list(list_temp);
free_list(s_list);
getch();
}
o_str("修改完毕,按任意键继续...");
getch();
return 0;
}