Advertisement
3axap_010

file2.cpp

Jun 20th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.03 KB | None | 0 0
  1. // file10.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  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.     do
  24.     {
  25.         i = scanf_s("%d", &n);
  26.         if (!i)
  27.         {
  28.             rewind(stdin);
  29.             continue;
  30.         }
  31.     } while (!i);
  32.  
  33.     return n;
  34. }
  35.  
  36. void file_fill(FILE* file)
  37. {
  38.     int i;
  39.  
  40.     for (;;)
  41.     {
  42.         i = get_int();
  43.  
  44.         if (i == 999)
  45.         {
  46.             break;
  47.         }
  48.  
  49.         fwrite(&i, sizeof(int), 1, file);
  50.     }
  51. }
  52.  
  53. void file_output(FILE* file)
  54. {
  55.     rewind(file);
  56.     int i;
  57.  
  58.     while (1)
  59.     {
  60.         fread(&i, sizeof(int), 1, file);
  61.         if (feof(file))
  62.         {
  63.             break;
  64.         }
  65.  
  66.         fprintf(stdout, "%d ", i);
  67.     }
  68.  
  69.     fprintf(stdout, "\n");
  70. }
  71.  
  72. void add(FILE* file)
  73. {
  74.     int i, j;
  75.  
  76.     fseek(file, 0, SEEK_END);
  77.     fpos_t end = ftell(file);
  78.     fpos_t pos;
  79.  
  80.     while (1)
  81.     {
  82.         file_output(file);
  83.  
  84.         i = get_int();
  85.         if (i == 999)
  86.         {
  87.             break;
  88.         }
  89.  
  90.         rewind(file);
  91.  
  92.         do
  93.         {
  94.             fread(&j, sizeof(int), 1, file);
  95.             if (feof(file))
  96.             {
  97.                 break;
  98.             }
  99.         } while (j < i);
  100.  
  101.         if (feof(file))
  102.         {
  103.             fwrite(&i, sizeof(int), 1, file);
  104.         }
  105.         else
  106.         {
  107.             fseek(file, -4, SEEK_CUR);
  108.  
  109.             while (1)
  110.             {
  111.                 fgetpos(file, &pos);
  112.                 if (pos == end)
  113.                 {
  114.                     fwrite(&i, sizeof(int), 1, file);
  115.                     end += 4;
  116.                     break;
  117.                 }
  118.  
  119.                 fread(&j, sizeof(int), 1, file);
  120.  
  121.                 fseek(file, -4, SEEK_CUR);
  122.                 fwrite(&i, sizeof(int), 1, file);
  123.                 i = j;
  124.             }
  125.         }
  126.     }
  127. }
  128.  
  129. int main()
  130. {
  131.     FILE* file = file_create("bin", "w+b");
  132.  
  133.     file_fill(file);
  134.  
  135.     file_output(file);
  136.  
  137.     add(file);
  138.  
  139.     file_output(file);
  140.  
  141.     fclose(file);
  142.  
  143.     return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement