第一种:
#include?<stdio.h>#include?<string.h>
#include?<stdlib.h>
struct?student?//结构体
{
char?name[20];//姓名 int?english;?//英语 int?math;?//数学? int?program;//程序设计}s[50];
void?daoru(struct?student?s[],?int*?n)?//文件导入函数
{
FILE?*p; int?i=*n; if((p=fopen("students.txt",?"r"))==NULL) {printf("无法打开此文件!");
} else {while(!feof(p))
{
fscanf(p,?"%s%d%d%d",?s[i].name,?&s[i].english,?&s[i].math,?&s[i].program); i++; *n=*n+1;}
} fclose(p);}
void?paixu(struct?student?s[],?int?n)?//排序函数
{
int?i,?j; struct?student?stu; int?allscore[2]; for?(i=0;?i<n-1;?i++) {for?(j=i+1;?j<n;?j++)
{
allscore[0]=s[i].english+s[i].math+s[i].program; allscore[1]=s[j].english+s[j].math+s[j].program; if?(allscore[0]<allscore[1]) {stu=s[i];
s[i]=s[j];
s[j]=stu;
}}
}}
void?dayin(struct?student?s[],?int?n)//显示所有信息
{
int?i; printf("\n姓名\t英语\t数学\t程序设计\t总分\n"); for?(i=0;?i<n;?i++) {printf("%s\t%d\t%d\t%d\t\t%d\n",s[i].name,?s[i].english,?s[i].math,?s[i].program,?(s[i].english+s[i].math+s[i].program));
}}
int?main()//主函数
{
int?k,?n=0; daoru(s,?&n); paixu(s,?n); dayin(s,?n); return?0;}
输出结果:
第二种二进制的导入也差不多,这里就不写了