wohyperion

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

Dec 15th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. /*
  2.  * 15.12.2016
  3.  * Обработка динамического списка std::list
  4.  * Запись и чтение его элементов в двоичный файл
  5. */
  6.  
  7. #include <iostream>
  8. #include <list>
  9. #include <fstream>
  10.  
  11. using namespace std;
  12.  
  13. int main()
  14. {
  15.     fstream bin("bin.txt", ios_base::binary | ios_base::out | ios_base::in | ios_base::app);
  16.  
  17.     int temp;
  18.     list<int> l;
  19.  
  20.     cout << "Input numbers: ";
  21.     while (cin >> temp)
  22.         l.push_back(temp);
  23.  
  24.     for (list<int>::iterator i = l.begin(); i != l.end(); i++)
  25.     {
  26.         temp = *i;
  27.         bin.write((char*)&temp, sizeof(int));
  28.     }
  29.  
  30.     list<int> rl;
  31.     bin.seekg(0, ios_base::beg);
  32.     while (true)
  33.     {
  34.         bin.read((char*)&temp, sizeof(int));
  35.         if (bin.eof()) break;
  36.         rl.push_back(temp);
  37.     }
  38.  
  39.     bin.close();
  40.  
  41.     cout << "List from binary file: ";
  42.     for (list<int>::iterator i = rl.begin(); i != rl.end(); i++)
  43.         cout << *i << ' ';
  44.  
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment