Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <list>
- #include <cstdlib>
- #include <fstream>
- #include <string>
- #include <Windows.h>
- using namespace std;
- void writeFile(list<string*> l), writeList(list<string*> l), printList(list<string*> l);
- list<string*> readInList(), formList(), delElem(list<string*> l, string* num);
- int main()
- {
- //setlocale(LC_ALL, "russian");
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- list<string*> l = formList();
- writeList(l);
- list<string*> k = readInList();
- printList(k);
- /*string temp;
- cout << "Введите элемент для удаления: ";
- cin >> temp;
- k = delElem(k, temp);
- writeFile(k);
- list<string> p = readInList();
- printList(p);*/
- system("pause");
- return 0;
- }
- list<string*> formList() // функция формирования списка
- {
- list<string*> l;
- string* temp;
- cout << "Ввод значений : " << endl;
- while (getline(cin, *temp))
- l.push_back(temp);
- return l;
- }
- void writeList(list<string*> l) // функция записи списка в двоичный файл
- {
- fstream bin("bin.txt", ios_base::binary | ios_base::out | ios_base::app);
- string* temp;
- for (list<string*>::iterator i = l.begin(); i != l.end(); i++)
- {
- temp = *i;
- //bin.write(temp.c_str(), temp.size());
- bin << *temp;
- }
- bin.close();
- }
- list<string*> readInList() //функция чтения из двоичного файла
- {
- string* temp;
- ifstream bin("bin.txt", ios_base::binary); //открыли файл
- list<string*> rl; // создали список
- char* str = new char[255];
- while (true) // пока верно считываем в файл
- {
- //bin.read((char*)&temp, sizeof(string)); // втение в файл
- //getline(bin, temp);
- //bin >> temp;
- bin >> *temp;
- if (bin.eof()) break; // пока не конец файла
- rl.push_back(temp); // записать в новый список
- }
- bin.close();
- return rl; //вернуть новый список
- }
- void printList(list<string*> l) //функция вывода списка на экран
- {
- int i = 1;
- cout << "Вывод значений: ";
- for (list<string*>::iterator it = l.begin(); it != l.end(); it++)
- cout << i++ << ". " << *it << endl;
- cout << endl;
- }
- list<string*> delElem(list<string*> l, string* num) //функция удаления элемента из списка
- {
- for (list<string*>::iterator i = l.begin(); i != l.end(); i++)
- {
- if (*i == num)
- {
- l.pop_back();
- break;
- }
- }
- return l;
- }
- void writeFile(list<string*> l)
- {
- fstream bin("bin.txt", ios_base::binary | ios_base::out | ios_base::trunc);
- string* temp;
- for (list<string*>::iterator i = l.begin(); i != l.end(); i++)
- {
- temp = *i;
- bin.write((char*)&temp, sizeof(string));
- }
- bin.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement