Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <algorithm>
- using namespace std;
- struct Book {
- string name;
- string author;
- int year;
- };
- class BinaryFiles {
- ifstream fin;
- ofstream fout;
- public:
- BinaryFiles(string file_name_first, string file_name_second) {
- fin.open(file_name_first, ios::binary);
- fout.open(file_name_second, ios::binary);
- }
- ~BinaryFiles() {
- fin.close();
- fout.close();
- }
- ifstream& GetIn() {
- return fin;
- }
- ofstream& GetOut() {
- return fout;
- }
- };
- void operator>>(BinaryFiles& files, Book& book) {
- files.GetIn().read((char*)&book, sizeof(book));
- }
- void operator<<(BinaryFiles& files, Book& book) {
- files.GetOut().write((char*)&book, sizeof(book));
- }
- int main() {
- int n;
- cout << "Enter number of books\n";
- cin >> n;
- BinaryFiles files1("file1.txt", "file1.txt");
- BinaryFiles files2("file2.txt", "file2.txt");
- for (int i = 0; i < n; ++i) {
- Book book;
- cout << "Enter name of book: ";
- cin >> book.name;
- cout << "Enter name of author: ";
- cin >> book.author;
- cout << "Enter year of book: ";
- cin >> book.year;
- files1 << book;
- }
- int year;
- cout << "Enter some year: ";
- cin >> year;
- for (int i = 0; i < n; ++i) {
- Book book;
- files1 >> book;
- if (book.year > year) {
- // cout << book.name << " " << book.author << " " << book.year << "\n";
- files2 << book;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement