Advertisement
3axap_010

files_merdge

Jun 16th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.67 KB | None | 0 0
  1. // file7.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  2. //Дано 2 текстовых файла, отсортированных по возрастанию. Сделать один, отсортированный по убыванию
  3.  
  4. #include "pch.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. FILE* file_create(const char* filename, const char* mode)
  9. {
  10.     FILE* file = fopen(filename, mode);
  11.     if (!file)
  12.     {
  13.         fprintf(stderr, "Can't open file\n");
  14.         return NULL;
  15.     }
  16.  
  17.     return file;
  18. }
  19.  
  20. int get_int()
  21. {
  22.     int i, n;
  23.  
  24.     do
  25.     {
  26.         i = scanf_s("%d", &n);
  27.         if (!i)
  28.         {
  29.             rewind(stdin);
  30.             continue;
  31.         }
  32.     } while (!i);
  33.    
  34.     return n;
  35. }
  36.  
  37. void file_fill(FILE* file)
  38. {
  39.     int i;
  40.  
  41.     for (;;)
  42.     {
  43.         i = get_int();
  44.  
  45.         if (i == 999) break;
  46.  
  47.         fprintf(file, "%2d", i);
  48.     }
  49. }
  50.  
  51. FILE* new_file(FILE* file1, FILE* file2)
  52. {
  53.     FILE* file3 = file_create("file3.txt", "w+");
  54.  
  55.     fseek(file1, -2, SEEK_END);
  56.     fseek(file2, -2, SEEK_END);
  57.  
  58.     fpos_t pos1, pos2;
  59.  
  60.     int i, j;
  61.  
  62.     while (1)
  63.     {
  64.         fgetpos(file1, &pos1);
  65.         fgetpos(file2, &pos2);
  66.  
  67.         fscanf_s(file1, "%d", &i);
  68.         fscanf_s(file2, "%d", &j);
  69.  
  70.         if (i >= j)
  71.         {
  72.             fprintf(file3, "%2d", i);
  73.             fsetpos(file2, &pos2);
  74.  
  75.             if (pos1 >= 2)
  76.             {
  77.                 fsetpos(file1, &pos1);
  78.                 fseek(file1, -2, SEEK_CUR);
  79.             }
  80.             else
  81.             {  
  82.                 pos1 = -1;
  83.                 break;
  84.             }
  85.         }
  86.         else
  87.         {
  88.             fprintf(file3, "%2d", j);
  89.             fsetpos(file1, &pos1);
  90.  
  91.             if (pos2 >= 2)
  92.             {
  93.                 fsetpos(file2, &pos2);
  94.                 fseek(file2, -2, SEEK_CUR);
  95.             }
  96.             else
  97.             {
  98.                 pos2 = -1;
  99.                 break;
  100.             }      
  101.         }
  102.     }
  103.  
  104.     if (pos1 >= 0)
  105.     {
  106.         while (pos1 >= 0)
  107.         {
  108.             fscanf_s(file1, "%d", &i);
  109.             fprintf(file3, "%2d", i);
  110.  
  111.             if (pos1 >= 2)
  112.             {
  113.                 fseek(file1, -4, SEEK_CUR);
  114.                 fgetpos(file1, &pos1);
  115.             }
  116.             else
  117.             {
  118.                 break;
  119.             }
  120.         }
  121.     }
  122.  
  123.     if (pos2 >= 0)
  124.     {
  125.         while (pos2 >= 0)
  126.         {
  127.             fscanf_s(file2, "%d", &j);
  128.             fprintf(file3, "%2d", j);
  129.  
  130.             if (pos2 >= 2)
  131.             {
  132.                 fseek(file2, -4, SEEK_CUR);
  133.                 fgetpos(file2, &pos2);
  134.             }
  135.             else
  136.             {
  137.                 break;
  138.             }
  139.         }
  140.     }
  141.  
  142.     return file3;
  143. }
  144.  
  145. void file_output(FILE* file)
  146. {
  147.     rewind(file);
  148.  
  149.     int i;
  150.  
  151.     while (!feof(file))
  152.     {
  153.         fscanf_s(file, "%d", &i);
  154.         fprintf(stdout, "%d ", i);
  155.     }
  156.  
  157.     fprintf(stdout, "\n");
  158. }
  159.  
  160. int main()
  161. {
  162.     FILE* file1 = file_create("file1.txt", "w+");
  163.     FILE* file2 = file_create("file2.txt", "w+");
  164.  
  165.     file_fill(file1);
  166.     file_fill(file2);
  167.  
  168.     file_output(file1);
  169.     file_output(file2);
  170.  
  171.     FILE* file3 = new_file(file1, file2);
  172.  
  173.     file_output(file3);
  174.  
  175.     fclose(file1);
  176.     fclose(file2);
  177.     fclose(file3);
  178.  
  179.     return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement