wohyperion

Динамический список и двоичные файлы

Dec 15th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     list<int> l;
  10.  
  11.     int temp, size;
  12.     cout << "Input numbers: ";
  13.     while (cin >> temp)
  14.         l.push_back(temp);
  15.  
  16.     size = l.size();
  17.  
  18.     ofstream out("bin.txt", ios_base::binary | ios_base::trunc);
  19.     out.write((char*)&size, sizeof(int));
  20.     for (list<int>::iterator i = l.begin(); i != l.end(); i++)
  21.     {
  22.         temp = *i;
  23.         out.write((char*)&temp, sizeof(int));
  24.     }
  25.     out.close();
  26.  
  27.     list<int> nl;
  28.     ifstream in("bin.txt", ios_base::binary);
  29.     in.read((char*)&size, sizeof(int));
  30.     for (int i = 0; i < size; i++)
  31.     {
  32.         in.read((char*)&temp, sizeof(int));
  33.         nl.push_back(temp);
  34.     }
  35.     in.close();
  36.  
  37.     cout << "Numbers read from binary file: ";
  38.     for (list<int>::iterator i = nl.begin(); i != nl.end(); i++)
  39.         cout << *i << ' ';
  40.     cout << endl;
  41.  
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment