• 一个没有验证的学生文件存储代码(试验报告性质)


    #include<stdio.h>
    #include<stdlib.h>
    struct student
    {
     char ID[20];
     char name[30];
     int age;
     double score;
    }stu[50];
    int writeIn(FILE *fpw);
    void ReadOut(FILE *fpr,int n);
    void Sort(struct student* stu,int n);
    int main()
    {
        FILE *fpr;
        FILE *fpw;
        if(!(fpw=fopen("D:\Info.dat","w+")))
        {
            printf("false to open the file! ");
            exit(1);
        }
        if(!(fpr=fopen("D:\Info.dat","r+")))
        {
            printf("false to open the file! ");
            exit(1);
        }
        printf(" ");
     int n=writeIn(fpw);
     ReadOut(fpr,n);
     Sort(stu,n);
     n=writeIn(fpw);
     ReadOut(fpr,n);
        fclose(fpw);
        fclose(fpr);
        return 0;
    }
    int writeIn(FILE *fpw)
    {
     for(int i=0;i<50;i++)
     {
      scanf("%s%s%d%lf",&stu[i].ID,&stu[i].name,&stu[i].age,&stu[i].score);
      if(stu[i].ID==NULL)
      {
       break;
      }
      fwrite(&stu[i].ID,sizeof(stu[i].ID),1,fpw);
      fwrite(&stu[i].name,sizeof(stu[i].name),1,fpw);
      fwrite(&stu[i].age,sizeof(stu[i].age),1,fpw);
      fwrite(&stu[i].score,sizeof(stu[i].score),1,fpw);
     }
     return i;//返回学生个数
    }
    void ReadOut(FILE *fpr,int n)//这个代码是cc过来的所以。。这里设计不好
    {
     for(int i=0;i<n-1;i++)
     {
      fread(&stu[i].ID,sizeof(stu[i].ID),1,fpr);
      fread(&stu[i].name,sizeof(stu[i].name),1,fpr);
      fread(&stu[i].age,sizeof(stu[i].age),1,fpr);
      fread(&stu[i].score,sizeof(stu[i].score),1,fpr);
      printf("%s  %s  %d  %lf ",stu[i].ID,stu[i].name,stu[i].age,stu[i].score);
     }
    }
    void Sort(struct student* stu,int n)
    {
     int k;
     struct student tp;
     for(int i=0;i<n-2;i++)
     {
      k=i;
      for(int j=0;j<n-2;j++)
      {
       if(stu[j+1].score<stu[k].score)
       {
        k=j+1;
       }
      }
      tp=stu[i];
      stu[i]=stu[k];
      stu[k]=tp;
     }
    }

  • 相关阅读:
    slice,substr和substring的区别
    http请求方法
    Git常用命令
    IOS开发之NSLog使用技巧
    网络请求之get与post异步请求
    通知(广播)传值
    [转]->ios推送:本地通知UILocalNotification
    [转]iOS 不要使用tag传递TableViewCell的indexPath值
    UIWebView uiwebview 加载本地html
    [转]字符串编码转换
  • 原文地址:https://www.cnblogs.com/zuoguangxing/p/3504206.html
Copyright © 2020-2023  润新知