排序好后写入文件

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 
  5 struct person{
  6         char *number;
  7         char *name;
  8         int score;
  9 };
 10 
 11 int main()
 12 {
 13         struct person arr[5];
 14         FILE *fp = fopen("src", "r");
 15         FILE *fp1 = fopen("dest", "a+");
 16         if(NULL == fp)
 17         {
 18                 perror("fopen");
 19                 return 1;
 20         }
 21         char *line = NULL;
 22         int len = 0, i = 0, j = 0;
 23         char pn[5][50] = {0};
 24 
 25         while ((getline(&line, &len, fp)) != -1)
 26         {
 27                 j = 0;
 28                 while(*line != '\n'){
 29                                 *(*(pn+i)+j) = *line++;
 30                                 j++;
 31                 }
 32                 i++;
 33         }
 34 
 35         char *str;
 36         int m = i;
 37         char *brr[3];
 38         char *tmp;
 39         for(i=0; i<m; i++)
 40         {
 41                 tmp = *(pn+i);
 42                 for(j=0; j<3; j++){
 43                         str = strstr(tmp, "\t");
 44                         *(brr+j) = strtok(tmp, "\t");
 45                         tmp = str + 1;
 46                 }
 47                 arr[i].number = *(brr+0);
 48                 arr[i].name = *(brr+1);
 49                 arr[i].score = atoi(*(brr+2));
 50                 printf("%s %s %d\n", arr[i].number, arr[i].name, arr[i].score);
 51         }
 52 
 53         struct person ttmp;
 54         for(i=0; i<m; i++)
 55         {
 56                 for(j=i; j<m; j++)
 57                 {
 58                         if(arr[i].score < arr[j].score)
 59                         {
 60                                 ttmp = arr[i];
 61                                 arr[i] = arr[j];
 62                                 arr[j] = ttmp;
 63                         }
 64                 }
 65         }
 66 
 67         char show[20];
 68         for(i=0;i<m;i++){
 69                 fputs(arr[i].number,fp1);       fputs("\t",fp1);
 70                 fputs(arr[i].name,fp1);         fputs("\t",fp1);
 71                 sprintf(show,"%d",arr[i].score);
 72                 fputs(show,fp1);                fputs("\n",fp1);
 73         }
 74 
 75         fclose(fp);
 76         fclose(fp1);
 77 }

 

转载于:https://www.cnblogs.com/luoxiang/p/4046535.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值