Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- using namespace std;
- void zabiv(FILE*&f) {
- int n, x;
- scanf("%d", &n);
- for (int i = 0; i < n; i++)
- {
- scanf("%d", &x);
- fwrite(&x, sizeof(int), 1, f);
- }
- fseek(f, 0, SEEK_SET);
- }
- void printer(FILE*&f)
- {
- fseek(f, 0, SEEK_SET);
- cout << endl;
- int kaka;
- while (fread(&kaka,sizeof(int),1,f))
- {
- cout << kaka << " ";
- }
- cout << endl;
- }
- int main()
- {
- FILE *f = fopen("file.bin", "wb+");
- if (f == NULL) {
- printf("Error opening file");
- exit(1);
- }
- //printer(f);
- fseek(f, 0, SEEK_SET);
- zabiv(f);
- int n, p;
- int pre, next;
- scanf("%d", &n);
- bool s;
- for (int i = 0; i < n; i++) {
- s = false;
- scanf("%d", &p);
- fseek(f, 0, SEEK_SET);
- while (!s && !feof(f)) {
- fread(&next, sizeof(int), 1, f);
- if (next >= p) {
- s = true;
- fseek(f, ftell(f)-sizeof(int), SEEK_SET);
- fwrite(&p, sizeof(int), 1, f);
- fseek(f, ftell(f) + sizeof(int), SEEK_SET);
- fseek(f, ftell(f) - sizeof(int), SEEK_SET);
- while (!feof(f)) {
- if (!fread(&pre, sizeof(int), 1, f)) { fwrite(&next, sizeof(int), 1, f); s = true; break; }
- fseek(f, ftell(f) - sizeof(int), SEEK_SET);
- fwrite(&next, sizeof(int), 1, f);
- fseek(f, ftell(f) + sizeof(int), SEEK_SET);
- fseek(f, ftell(f) - sizeof(int), SEEK_SET);
- next = pre;
- }
- }
- }
- if (!s) {
- fwrite(&p, sizeof(int), 1, f);
- }
- }
- printer(f);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement