feof很好用但是我用的不多

10-76的作业

#include<stdio.h>
struct student
{
    int num;
    char name[20];
    int score;
};
int main()
{
    int i;
   struct student st,stmax,stmin;
    FILE *fp;
    stmax.score=0; stmin.score=100;
    fp=fopen("file1.dat","r");
    if(!fp) return 1;
    while(!feof(fp))/*和if(i=0;i<10;i++)效果一样,但是如果事先不知道学生个数if就用不了了*/
    {
        fscanf(fp,"%d%s%d",&st.num,st.name,&st.score);
        if(st.score>stmax.score)
            stmax=st;
        if(st.score<stmin.score)
            stmin=st;
    }
    fclose(fp);
    printf("hight:%5d%15s%5d",stmax.num,stmax.name,stmax.score);
    printf("\n low:%5d%15s%5d",stmin.num,stmin.name,stmin.score);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/a846276245/article/details/85926946