Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // file8.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
- // В бинарном файле поменять местами 1 и 2, 3 и 4 элементы и т.д.
- #include "pch.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- FILE* file_create(const char* filename, const char* mode)
- {
- FILE* file = fopen(filename, mode);
- if (!file)
- {
- fprintf(stderr, "Can't open file\n");
- return NULL;
- }
- return file;
- }
- int get_int()
- {
- int i, n;
- do
- {
- i = scanf_s("%d", &n);
- if (!i)
- {
- rewind(stdin);
- continue;
- }
- } while (!i);
- return n;
- }
- void file_fill(FILE* file)
- {
- int i;
- for (;;)
- {
- i = get_int();
- if (i == 999) break;
- fwrite(&i, sizeof(int), 1, file);
- }
- }
- void file_output(FILE* file)
- {
- rewind(file);
- fprintf(stdout, "\n");
- int c;
- while (1)
- {
- fread(&c, sizeof(int), 1, file);
- if (feof(file)) break;
- fprintf(stdout, "%d ", c);
- }
- fprintf(stdout, "\n");
- }
- void file_change(FILE* file)
- {
- int c, c1;
- fseek(file, 0, SEEK_END);
- fpos_t pos = ftell(file);
- rewind(file);
- fpos_t cur_pos = fgetpos(file, &cur_pos);
- while (1)
- {
- fread(&c, sizeof(int), 1, file);
- fgetpos(file, &cur_pos);
- if (cur_pos >= pos) break;
- fread(&c1, sizeof(int), 1, file);
- fgetpos(file, &cur_pos);
- fseek(file, -8, SEEK_CUR);
- fwrite(&c1, sizeof(int), 1, file);
- fwrite(&c, sizeof(int), 1, file);
- if (cur_pos >= pos) break;
- }
- }
- int main()
- {
- FILE* file = file_create("bin", "wb+");
- file_fill(file);
- file_change(file);
- file_output(file);
- fclose(file);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement