实现功能:
1.输入信息
2.打印信息
3.保存信息
4.加载所有信息,并打印
5.所存学生信息按年龄排序
6.查找学生并修改信息
7.删除学生信息
8.查看文件的大小
9.退出
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STD 100 //最大容量
#define SUB_NUM 3 // 科目数
#define HOB 2 //爱好
#define Data_path "../data/stu_information" //学生信息文件
#define SORT_DATA "../data/.sortemp" //排序中间文件
#define NO_change "../data/.source_file" //记录文件,每次输入的数据都存在这里
#define Temp_file "../data/.temp_file" //中间文件
typedef struct
{
char name[50];
int age;
float score[3];
char hobby[2][50];
}Std;
void menu(void)
{
printf("请选择您的操作:\n");
printf("1--------------录入信息\n");
printf("2--------------打印信息\n");
printf("3--------------保存信息\n");
printf("4--------------加载信息\n");
printf("5--------------排序\n");
printf("6--------------修改信息\n");
printf("7--------------删除信息\n");
printf("8--------------文件大小\n");
printf("-1--------------退出系统\n");
}
void Save (Std *lp);
void Input(Std *pres,int src_size)
{
Std *lp = malloc(sizeof(Std) * 1 ) ;
memset(lp,0,sizeof(Std) * 1 );
char opt;
printf("请输入学员信息--姓名--年龄--成绩--爱好");
scanf("%s %d",lp->name,&lp->age);
for (int i=0;i<src_size;i++)
{
scanf("%f",&lp->score[i]);
}
for (int i=0;i<HOB;i++)
{
scanf("%s",lp->hobby[i]);
}
getchar();
printf("是否保存信息?\n");
scanf("%c",&opt);
if ('Y'==opt || 'y' == opt)
{
Save(lp);
}
else
{
*pres = *lp;
}
}
void Output(Std *lp,int src_size)
{
if (*lp->name =='\0' )
{
perror("NO input!");
}
printf("姓名:%s\t年龄:%d\n",lp->name,lp->age);
printf("\n成绩");
for (int i=0;i<src_size;i++)
{
printf("%.2f\t",lp->score[i]);
}
printf("\n爱好");
for (int i=0;i<2;i++)
{
printf("%s\n",lp->hobby[i]);
}
}
void Save(Std *lp)
{
FILE *pw=NULL;
FILE *ptw=NULL;
FILE *pc;
pw = fopen(Data_path,"a+");
ptw = fopen(NO_change,"a+");
if (pw == NULL)
{
perror("\aopen file error!");
return ;
}
if (ptw == NULL)
{
perror("\aopen temp file error!");
return ;
}
if ('\0' == *lp->name)
{
printf("\a没有输入信息,无法保存!");
return ;
}
fwrite(lp,sizeof(Std),1,pw);
fwrite(lp,sizeof(Std),1,ptw);
fclose(pw);
fclose(ptw);
printf("学员信息录入成功");
}
void Load(Std *psum,int src_size)
{
int data_num=0;
FILE *pr = NULL;
pr = fopen(Data_path,"r");
if(NULL == pr)
{
perror("open error");
return ;
}
while(1)
{
Std *pt=malloc(sizeof(Std)*1);
memset(pt,0,sizeof(Std)*1);
fread(pt,sizeof(Std),1,pr);
*(psum+data_num) = *pt;
if (feof(pr))
{
printf("数据加载完毕!");
return ;
}
data_num++;
printf("姓名:%s\t年龄:%d",pt->name,pt->age);
printf("\n成绩:");
for (int i=0;i<src_size;i++)
{
printf("%.2f\t",pt->score[i]);
}
printf("\n爱好:\n");
for (int i=0;i<2;i++)
{
printf("%s\n",pt->hobby[i]);
}
free(pt);
}
fclose(pr);
}
void Sort(int max_size)
{
Std temp = {0};
FILE *pr = fopen(Data_path,"r");
FILE *pw = fopen(SORT_DATA,"w");
Std *psum = malloc(sizeof(Std) * max_size);
memset(psum,0,sizeof(Std) * max_size);
int cur_sum=0;
if (NULL ==pr)
{
perror("open file error on readonly!");
return ;
}
if (NULL == pw)
{
perror("open file error on writeonly!");
return ;
}
while (1)
{
fread((psum+cur_sum),sizeof(Std),1,pr);
if (feof(pr))
{
break;
}
cur_sum++;
}
fclose (pr);
for (int i=0;i < cur_sum-1;i++)
{
for (int j = 0;j<cur_sum-i-1;j++)
{
if ((psum+j)->age > (psum+j+1)->age )
{
temp =*(psum+j);
*(psum+j) = *(psum+j+1);
*(psum+j+1) = temp;
}
}
}
fwrite(psum,sizeof(Std),cur_sum,pw);
remove(Data_path);
rename(SORT_DATA,Data_path);
fclose(pw);
free(psum);
}
int Count_data(Std *pal)
{
int count=0;
while (*(pal+count)->name!='\0')
{
count++;
}
printf("%d\n",count);
return count;
}
void Mind(Std *pal,int src_size)
{
int data_batch= Count_data(pal);
char f_name[50] = {0};
Std *pshr = malloc(sizeof(Std) * 1 );
memset(pshr,0,sizeof(Std) * 1 );
FILE *pw = fopen(Temp_file,"w");
if (pw == NULL)
{
perror("open temp_file error\n");
return;
}
printf("请输入要查找的学员姓名:");
scanf("%s",f_name);
for (int i=0;i<MAX_STD;i++)
{
if (*(pal+i)->name == '\0')
{
perror("\a查无此人!");
break;
}
if (strcmp((pal+i)->name,f_name) == 0 )
{
printf("请修改信息\n姓名--年龄--成绩---爱好");
scanf("%s",pshr->name);
scanf("%d",&pshr->age);
for (int j=0;j<src_size;j++)
{
scanf("%f",&pshr->score[j]);
}
for (int n=0;n<2;n++)
{
scanf("%s",pshr->hobby[n]);
}
*(pal+i) = *pshr;
break;
}
}
fwrite(pal,sizeof(Std),data_batch,pw);
fclose(pw);
remove(Data_path);
rename(Temp_file,Data_path);
}
void Del_data(void)
{
int flag = 1;
char del_name[50] = {0};
Std *pshot = malloc(sizeof(Std) *1 );
memset(pshot,0,sizeof(Std));
FILE *pw = NULL,*pr = NULL;
pr = fopen(Data_path,"r");
pw = fopen(Temp_file,"w");
if ( NULL==pr )
{
perror("open source file error");
return;
}
if ( NULL==pw )
{
perror("open temp file error");
return;
}
printf("请输入要删除的学员姓名:\n");
scanf("%s",del_name);
while (1)
{
fread(pshot,sizeof(Std),1,pr);
if (feof(pr))
{
break;
}
if (strcmp(pshot->name,del_name) == 0)
{
flag = 0;
printf("删除成功!\n");
continue;
}
fwrite(pshot,sizeof(Std),1,pw);
}
fclose(pr);
fclose(pw);
free(pshot);
if (flag)
{
printf("查无此人!\n");
}
remove(Data_path);
rename(Temp_file,Data_path);
}
void File_size()
{
int size = 0;
FILE *pr = NULL;
char file_path[150] = {0};
printf("请输入要查看的文件路径:\n");
scanf("%s",file_path);
getchar();
pr = fopen(file_path,"r");
if (NULL == pr)
{
perror("open file error");
return;
}
fseek(pr,0,SEEK_END);
size=ftell(pr);
pclose(pr);
printf("当前文件大小为%d",size);
}
int main()
{
int opt=0;
Std *pall = malloc(sizeof(Std)*MAX_STD);
memset(pall,0,sizeof(Std) *MAX_STD);
Std *pone = malloc(sizeof(Std)*1);
memset(pone,0,sizeof(Std) *1);
while(1)
{
menu();
scanf("%d",&opt);
if ('\n' != getchar() )
{
continue;
}
if (opt == -1)
{
printf("退出系统成功!\n");
break;
}
switch (opt)
{
case 1 : Input(pone,SUB_NUM);break;
case 2 : Output(pone,SUB_NUM);break;
case 3 : Save(pone);break;
case 4 : Load(pall,SUB_NUM);break;
case 5 : Sort(MAX_STD);break;
case 6 : Mind(pall,SUB_NUM);break;
case 7 : Del_data();break;
case 8 : File_size() ;break;
}
}
free (pone);
free (pall);
}