Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include < fstream >
- #include < conio.h >
- #include < iostream >
- #include < string >
- #include < windows.h >
- using namespace std;
- void print_mass(int* mass, int m) {
- for (int i = 0; i < m; i++)
- {
- cout << mass[i] << " ";
- }
- cout << endl;
- }
- int main() {
- setlocale(LC_ALL, "rus");
- int case_num,check=0,n,m=0,t;
- char a;
- char file_name_out[40];
- char file_name_in[40];
- while (true) {
- cout << "Введите номер команды\n";
- cin >> case_num;
- switch (case_num) {
- case 1: //записывает введенный с клавиатура массив в бинарный файл
- {
- fstream fout;
- cout << "Введите количество эл\n";
- cin >> n;
- int *input_m = new int[n]; //объвление динамического массива
- cout << "Введите сами элементы\n";
- for (int i = 0; i < n; i++) cin >> input_m[i];
- while (!fout.is_open()) {
- cout << "Введите название файла\n";
- cin >> file_name_out;
- fout.open(file_name_out, ios::out | ios::binary);
- }
- for (int i = 0; i < n; i++)
- {
- fout.write((char*)&input_m[i], sizeof(input_m[i])); //переводим в строку байтов, потом указываем количество байтов, сколько будем записывать
- }
- print_mass(input_m, n);
- //проверка если файл уже существует
- break;
- }
- case 2:
- {
- fstream fin;
- while (check == 0) {
- cout << "Введите название файла\n";
- cin >> file_name_in;
- fin.open(file_name_in, ios::in | ios::binary);
- if (!fin.is_open()) check = 0;
- else check = 1;
- }
- t = fin.tellg();
- while (fin >> a) { //считает сколько чисел удалось прочитать
- printf("%x ", a);
- //cout << int(a) << endl;
- t = fin.tellg();
- m++; //сколько мы смогли считать из файла
- }
- cout << m << endl;
- char* buffer = new char[sizeof(int)];
- fin.clear(); //очищаем флаги
- fin.seekg(0); //перемещаем картку в начало
- int *output_m = new int[m / sizeof(int)]; //объвление динамического массива
- cout << m / sizeof(int) << endl;
- for (int i = 0; i < m/sizeof(int); i++)
- {
- fin.read(buffer, sizeof(int));
- cout << endl;
- for (int j = 0; j < 4; j++)
- {
- printf("%x ", buffer[j]);
- }
- output_m[i] = (int)buffer;
- fin.seekg(sizeof(int));
- }
- print_mass(output_m, m / sizeof(int));
- break;
- }
- case 0:
- return 0;
- default: {
- cout << "Вы ввели неправильный номер команды\n";
- }
- }
- }
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement