Advertisement
Dinmrmr

5 лаба, на 21,12,16

Dec 20th, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <cstdlib>
  4. #include <fstream>
  5. #include <string>
  6. #include <Windows.h>
  7.  
  8. using namespace std;
  9.  
  10. void writeFile(list<string*> l), writeList(list<string*> l), printList(list<string*> l);
  11. list<string*> readInList(), formList(), delElem(list<string*> l, string* num);
  12.  
  13. int main()
  14. {
  15.     //setlocale(LC_ALL, "russian");
  16.     SetConsoleCP(1251);
  17.     SetConsoleOutputCP(1251);
  18.  
  19.  
  20.     list<string*> l = formList();
  21.  
  22.     writeList(l);
  23.  
  24.     list<string*> k = readInList();
  25.    
  26.     printList(k);
  27.     /*string temp;
  28.     cout << "Введите элемент для удаления: ";
  29.     cin >> temp;
  30.     k = delElem(k, temp);
  31.     writeFile(k);
  32.    
  33.     list<string> p = readInList();
  34.     printList(p);*/
  35.  
  36.     system("pause");
  37.     return 0;
  38. }
  39.  
  40. list<string*> formList() // функция формирования списка
  41. {
  42.     list<string*> l;
  43.  
  44.     string* temp;
  45.     cout << "Ввод значений : " << endl;
  46.     while (getline(cin, *temp))
  47.         l.push_back(temp);
  48.  
  49.     return l;
  50. }
  51.  
  52. void writeList(list<string*> l) // функция записи списка в двоичный файл
  53. {
  54.     fstream bin("bin.txt", ios_base::binary | ios_base::out | ios_base::app);
  55.     string* temp;
  56.     for (list<string*>::iterator i = l.begin(); i != l.end(); i++)
  57.     {
  58.         temp = *i;
  59.         //bin.write(temp.c_str(), temp.size());
  60.         bin << *temp;
  61.     }
  62.     bin.close();
  63. }
  64.  
  65. list<string*> readInList() //функция чтения из двоичного файла
  66. {
  67.     string* temp;
  68.     ifstream bin("bin.txt", ios_base::binary); //открыли файл
  69.  
  70.     list<string*> rl; // создали список
  71.    
  72.     char* str = new char[255];
  73.  
  74.     while (true) // пока верно считываем в файл
  75.     {
  76.         //bin.read((char*)&temp, sizeof(string)); // втение в файл
  77.         //getline(bin, temp);
  78.         //bin >> temp;
  79.         bin >> *temp;
  80.         if (bin.eof()) break; // пока не конец файла
  81.         rl.push_back(temp); // записать в новый список
  82.     }
  83.     bin.close();
  84.  
  85.     return rl; //вернуть новый список
  86. }
  87.  
  88. void printList(list<string*> l) //функция вывода списка на экран
  89. {
  90.     int i = 1;
  91.     cout << "Вывод значений: ";
  92.     for (list<string*>::iterator it = l.begin(); it != l.end(); it++)
  93.         cout << i++ << ". " << *it << endl;
  94.     cout << endl;
  95. }
  96.  
  97. list<string*> delElem(list<string*> l, string* num) //функция удаления элемента из списка
  98. {
  99.  
  100.     for (list<string*>::iterator i = l.begin(); i != l.end(); i++)
  101.     {
  102.         if (*i == num)
  103.         {
  104.             l.pop_back();
  105.             break;
  106.         }
  107.     }
  108.     return l;
  109. }
  110.  
  111. void writeFile(list<string*> l)
  112. {
  113.     fstream bin("bin.txt", ios_base::binary | ios_base::out | ios_base::trunc);
  114.  
  115.     string* temp;
  116.     for (list<string*>::iterator i = l.begin(); i != l.end(); i++)
  117.     {
  118.         temp = *i;
  119.         bin.write((char*)&temp, sizeof(string));
  120.     }
  121.     bin.close();
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement