Advertisement
namereq

ファイル入出力とソート

Jul 9th, 2018
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.60 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int main(void)
  5. {
  6.     int i,j,n,a,str[500];
  7.    
  8.     FILE *fin, *fout;
  9.    
  10.     if ((fin = fopen("data1", "r")) == NULL)
  11.     {
  12.         printf("入力ファイルなし\n");
  13.         exit(1);
  14.     }
  15.     if ((fout = fopen("data2", "w")) == NULL)
  16.     {
  17.         printf("出力ファイルなし\n");
  18.         exit(1);
  19.     }
  20.     n = 0;
  21.     while (( str[n] = fgetc(fin)) != EOF){
  22.         n++;
  23.     }
  24.    
  25.     for (i=0;i<n;i++)
  26.     {
  27.         for (j=i+1;j<n;j++)
  28.         {
  29.             if(str[i]>str[j])
  30.             {
  31.                 a=str[i];
  32.                 str[i]=str[j];
  33.                 str[j]=a;
  34.             }
  35.         }
  36.         fputc(str[i],fout);
  37.     }
  38.     fclose(fin);
  39.     fclose(fout);
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement