Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include < fstream >
  3. #include < conio.h >
  4. #include < iostream >
  5. #include < string >
  6. #include < windows.h >
  7.  
  8. using namespace std;
  9.  
  10. void print_mass(int* mass, int m) {
  11.     for (int i = 0; i < m; i++)
  12.     {
  13.         cout << mass[i] << "  ";
  14.     }
  15.     cout << endl;
  16. }
  17.  
  18.  
  19. int main() {
  20.     setlocale(LC_ALL, "rus");
  21.  
  22.     int case_num,check=0,n,m=0,t;
  23.     char a;
  24.     char file_name_out[40];
  25.     char file_name_in[40];
  26.     while (true) {
  27.         cout << "Введите номер команды\n";
  28.         cin >> case_num;
  29.         switch (case_num) {
  30.         case 1: //записывает введенный с клавиатура массив в бинарный файл
  31.         {
  32.             fstream fout;
  33.             cout << "Введите количество эл\n";
  34.             cin >> n;
  35.             int *input_m = new int[n]; //объвление динамического массива
  36.             cout << "Введите сами элементы\n";
  37.             for (int i = 0; i < n; i++) cin >> input_m[i];
  38.             while (!fout.is_open()) {
  39.                 cout << "Введите название файла\n";
  40.                 cin >> file_name_out;
  41.                 fout.open(file_name_out, ios::out | ios::binary);
  42.             }
  43.             for (int i = 0; i < n; i++)
  44.             {
  45.                 fout.write((char*)&input_m[i], sizeof(input_m[i])); //переводим в строку байтов, потом указываем количество байтов, сколько будем записывать
  46.             }
  47.             print_mass(input_m, n);
  48.             //проверка если файл уже существует
  49.             break;
  50.         }
  51.         case 2:
  52.         {
  53.             fstream fin;
  54.             while (check == 0) {
  55.                 cout << "Введите название файла\n";
  56.                 cin >> file_name_in;
  57.                 fin.open(file_name_in, ios::in | ios::binary);
  58.                 if (!fin.is_open()) check = 0;
  59.                 else check = 1;
  60.             }
  61.             t = fin.tellg();
  62.             while (fin >> a) { //считает сколько чисел удалось прочитать
  63.                 printf("%x ", a);
  64.                 //cout << int(a) << endl;
  65.                 t = fin.tellg();
  66.                 m++;        //сколько мы смогли считать из файла
  67.             }
  68.  
  69.             cout << m << endl;
  70.  
  71.             char* buffer = new char[sizeof(int)];
  72.             fin.clear(); //очищаем флаги
  73.             fin.seekg(0); //перемещаем картку в начало
  74.             int *output_m = new int[m / sizeof(int)]; //объвление динамического массива
  75.  
  76.             cout << m / sizeof(int) << endl;
  77.  
  78.             for (int i = 0; i < m/sizeof(int); i++)
  79.             {
  80.                 fin.read(buffer, sizeof(int));
  81.                 cout << endl;
  82.                 for (int j = 0; j < 4; j++)
  83.                 {
  84.                     printf("%x ", buffer[j]);
  85.                 }
  86.                 output_m[i] = (int)buffer;
  87.                 fin.seekg(sizeof(int));
  88.             }
  89.             print_mass(output_m, m / sizeof(int));
  90.  
  91.             break;
  92.         }
  93.         case 0:
  94.             return 0;
  95.         default: {
  96.             cout << "Вы ввели неправильный номер команды\n";
  97.         }
  98.         }
  99.     }
  100.  
  101.     _getch();
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement