Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.99 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int
  4. main(void)
  5. {
  6.     FILE *f1, *f2;
  7.     int tmp, i, n, max, buf, j, fr, fw;
  8.     if ((f1 = fopen("input.txt", "rt")) == NULL) {
  9.         return 0;
  10.     }
  11.     if ((f2 = fopen("output.bin", "wb+")) == NULL) {
  12.         return 0;
  13.     }
  14.     if (fscanf(f1, "%d", &n) != 1) {
  15.         return 0;
  16.     }
  17.     fseek(f2, 0L, SEEK_SET);
  18.     for (i = 0; i < n; i++) {
  19.         if (fscanf(f1, "%d", &tmp)!= 1) {
  20.             return 0;
  21.         }
  22.         if (fwrite(&tmp, sizeof(int), 1, f2)!= 1) {
  23.             return 0;
  24.         }
  25.     }
  26.     for (i = n; i > 1; i--) {
  27.         fseek(f2, 0L, SEEK_SET);
  28.         for(j = 1; j < i; j++) {
  29.             fr = fread(&max, sizeof(int), 1, f2);
  30.             fr = fread(&buf, sizeof(int), 1, f2);
  31.             if (max > buf) {
  32.                 fseek(f2, -2*sizeof(int), SEEK_CUR);
  33.                 if (fwrite(&buf, sizeof(int), 1, f2)!= 1) {
  34.                     return 0;
  35.                 }
  36.                 if (fwrite(&max, sizeof(int), 1, f2)!= 1) {
  37.                     return 0;
  38.                 }
  39.             }
  40.             fseek(f2, -sizeof(int), SEEK_CUR);
  41.         }
  42.     }
  43.     fclose(f1);
  44.     fclose(f2);
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement