Advertisement
codegod313

Sasha_p_binary

Apr 29th, 2020
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7.  
  8.  
  9. void zabiv(FILE*&f) {
  10.     int n, x;
  11.  
  12.     scanf("%d", &n);
  13.     for (int i = 0; i < n; i++)
  14.     {
  15.         scanf("%d", &x);
  16.         fwrite(&x, sizeof(int), 1, f);
  17.     }
  18.     fseek(f, 0, SEEK_SET);
  19.  
  20. }
  21.  
  22.  
  23. void printer(FILE*&f)
  24. {
  25.     fseek(f, 0, SEEK_SET);
  26.     cout << endl;
  27.     int kaka;
  28.     while (fread(&kaka,sizeof(int),1,f))
  29.     {
  30.         cout << kaka << " ";
  31.     }
  32.     cout << endl;
  33. }
  34.  
  35.  
  36. int main()
  37. {
  38.     FILE *f = fopen("file.bin", "wb+");
  39.     if (f == NULL) {
  40.         printf("Error opening file");
  41.         exit(1);
  42.     }
  43.     //printer(f);
  44.     fseek(f, 0, SEEK_SET);
  45.     zabiv(f);
  46.     int n, p;
  47.     int pre, next;
  48.     scanf("%d", &n);
  49.     bool s;
  50.     for (int i = 0; i < n; i++) {
  51.         s = false;
  52.         scanf("%d", &p);
  53.         fseek(f, 0, SEEK_SET);
  54.         while (!s && !feof(f)) {
  55.             fread(&next, sizeof(int), 1, f);
  56.             if (next >= p) {
  57.                 s = true;
  58.                 fseek(f, ftell(f)-sizeof(int), SEEK_SET);
  59.                 fwrite(&p, sizeof(int), 1, f);
  60.                 fseek(f, ftell(f) + sizeof(int), SEEK_SET);
  61.                 fseek(f, ftell(f) - sizeof(int), SEEK_SET);
  62.                 while (!feof(f)) {
  63.                     if (!fread(&pre, sizeof(int), 1, f)) { fwrite(&next, sizeof(int), 1, f); s = true; break; }
  64.                     fseek(f, ftell(f) - sizeof(int), SEEK_SET);
  65.                     fwrite(&next, sizeof(int), 1, f);
  66.                     fseek(f, ftell(f) + sizeof(int), SEEK_SET);
  67.                     fseek(f, ftell(f) - sizeof(int), SEEK_SET);
  68.                     next = pre;
  69.                 }
  70.             }
  71.  
  72.         }
  73.         if (!s) {
  74.             fwrite(&p, sizeof(int), 1, f);
  75.         }
  76.  
  77.  
  78.     }
  79.     printer(f);
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement