Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <list>
- #include <fstream>
- using namespace std;
- int main()
- {
- list<int> l;
- int temp, size;
- cout << "Input numbers: ";
- while (cin >> temp)
- l.push_back(temp);
- size = l.size();
- ofstream out("bin.txt", ios_base::binary | ios_base::trunc);
- out.write((char*)&size, sizeof(int));
- for (list<int>::iterator i = l.begin(); i != l.end(); i++)
- {
- temp = *i;
- out.write((char*)&temp, sizeof(int));
- }
- out.close();
- list<int> nl;
- ifstream in("bin.txt", ios_base::binary);
- in.read((char*)&size, sizeof(int));
- for (int i = 0; i < size; i++)
- {
- in.read((char*)&temp, sizeof(int));
- nl.push_back(temp);
- }
- in.close();
- cout << "Numbers read from binary file: ";
- for (list<int>::iterator i = nl.begin(); i != nl.end(); i++)
- cout << *i << ' ';
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment