文件

#include<stdio.h>
#include<string.h>
#define SIZE 5
struct Student
{
char name[20];
int num;
char sex;
double sub1;
double sub2;
double sub3;
double aver;
};
void write_to_f1(FILE* fp,struct Student stu[])
{
int i;
double av;
if((fp=fopen("student.dat","wb"))==NULL)
{
printf("cannot open this file\n");
return;
}
for(i=0; i<SIZE; i++)
{
av=0;
scanf("%s",stu[i].name);
getchar();
scanf("%d",&stu[i].num);
getchar();
scanf("%c",&stu[i].sex);
getchar();
scanf("%lf%lf%lf",&stu[i].sub1,&stu[i].sub2,&stu[i].sub3);
getchar();
av=(stu[i].sub1+stu[i].sub2+stu[i].sub3)/3;
stu[i].aver=av;
}
for(i=0; i<SIZE; i++)
if(fwrite(&stu[i],sizeof(struct Student),1,fp)!=1)
printf("file write error\n");
fclose(fp);
}
void read_and_sort(FILE* fp,struct Student stu[])//¶ÁÈ¡Êý¾Ý²¢ÅÅÐò
{
int i,flag1,j;
struct Student term;
if((fp=fopen("student.dat","r"))==NULL)
{
printf("cannot open this file\n");
return;
}
for(i=0; i<SIZE; i++)
fread(&stu[i],sizeof(struct Student),1,fp);
for(i=0; i<SIZE-1; i++)
{
flag1=i;
for(j=flag1; j<SIZE; j++)
if(stu[j].aver>stu[flag1].aver)
flag1=j;
term=stu[i];
stu[i]=stu[flag1];
stu[flag1]=term;
}
fclose(fp);
}

void write_to_f2(FILE* fp,struct Student stu[])
{
int i;
if((fp=fopen("sort_stu.dat","w"))==NULL)
{
printf("cannot open this file\n");
return;
}
for(i=0; i<SIZE; i++)
{
printf("%-15s%-10d%c\t%6.1lf%6.1lf%6.1lf\t%6.1lf\n",stu[i].name,stu[i].num,stu[i].sex,stu[i].sub1,stu[i].sub2,stu[i].sub3,stu[i].aver);
fprintf(fp,"%-15s%-10d%c\t%6.1lf%6.1lf%6.1lf\t%6.1lf\n",stu[i].name,stu[i].num,stu[i].sex,stu[i].sub1,stu[i].sub2,stu[i].sub3,stu[i].aver);
}
fclose(fp);
}
int main()
{
FILE* fp;
struct Student stu[5];
//write_to_f1(fp,stu);
read_and_sort(fp,stu);
write_to_f2(fp,stu);
return 0;
}
/*ll 101 f 98 97 99
mm 102 f 77 99 99
nn 103 f 88 99 99
bb 104 f 67 99 99
kk 105 f 79 99 99 */

或:

#include<stdio.h>
#include<stdlib.h>
#define size 5
struct student
{
char name [10];
int num;
float score[3];
char sex;
float ave;
} stu[size],t;
void save()
{
FILE *fp;
int i;
if((fp=fopen("stu.dat","w"))==NULL)
{
printf("the file can not open \n");
return;
}
for(i=0; i<size ; i++)
/*if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1)
{
printf("the file write error\n");
return;
}*/
fprintf(fp,"%-10s %3d %7.2f %7.2f %7.2f %5c %7.2f\n",stu[i].name,stu[i].num,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].sex,stu[i].ave);
fclose(fp);
}
void sort()
{
FILE *fp1,*fp2;
int i,j;
if((fp1=fopen("stu.dat","r"))==NULL)
{
printf("the file can't open\n");
exit(0);
}
if((fp2=fopen("sort.dat","w"))==NULL)
{
printf("the file write error\n");
exit(0);
}
for (i=0; i<size; i++)
{
/*if(fread(&stu[i],sizeof(struct student ),1,fp1)!=1)
{
printf("file read errror\n");
exit(0);
}*/
fscanf(fp1,"%-10s %3d %7.2f %7.2f %7.2f %5c %7.2f\n",stu[i].name,&stu[i].num,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2],&stu[i].sex,&stu[i].ave);
}
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
if(stu[i].ave<stu[j].ave)
{
t=stu[i];
stu[i]=stu[j];
stu[j]=t;
}
fwrite(&stu[i],sizeof(struct student),1,fp2);
}
fclose(fp1);
fclose(fp2);
}

int main()
{
int i;
float sum[size];
FILE *fp1;
for(i=0; i<size; i++)
{
scanf("%s%d%f%f%f",stu[i].name,&stu[i].num,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
getchar();
scanf("%c",&stu[i].sex);
sum[i]=stu[i].score[0]+stu[i].score[1]+stu[i].score[2];
stu[i].ave=sum[i]/3.0;
}
save();
sort();
if((fp1=fopen("sort.dat","w"))==NULL)
{
printf("the file can't open");
exit(0);
}
fprintf(fp1,"sorted students are as fallow\n");
fprintf(fp1,"\nname no. score1 score2 score3 sex average\n");
for(i=0; i<size; i++)
{
printf("%-10s %3d %7.2f %7.2f %7.2f %5c %7.2f\n",stu[i].name,stu[i].num,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].sex,stu[i].ave);
fprintf(fp1,"%-10s %3d %7.2f %7.2f %7.2f %5c %7.2f\n",stu[i].name,stu[i].num,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].sex,stu[i].ave);
}
printf("the file has been writed in sort.dat");
fclose(fp1);
return 0;
}
/*ll 101 98 97 99 f
mm 102 77 99 99 f
nn 103 88 99 99 f
bb 104 67 99 99 f
kk 105 79 99 99 f*/

 

posted @ 2015-01-10 17:31  zach96  阅读(162)  评论(0编辑  收藏  举报