Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. struct Book {
  8.   string name;
  9.   string author;
  10.   int year;
  11. };
  12.  
  13. class BinaryFiles {
  14.   ifstream fin;
  15.   ofstream fout;
  16.  
  17.   public:
  18.     BinaryFiles(string file_name_first, string file_name_second) {
  19.       fin.open(file_name_first, ios::binary);
  20.       fout.open(file_name_second, ios::binary);
  21.     }
  22.    
  23.     ~BinaryFiles() {
  24.       fin.close();
  25.       fout.close();      
  26.     }
  27.    
  28.     ifstream& GetIn() {
  29.       return fin;
  30.     }
  31.    
  32.     ofstream& GetOut() {
  33.       return fout;
  34.     }
  35. };
  36.  
  37. void operator>>(BinaryFiles& files, Book& book) {
  38.   files.GetIn().read((char*)&book, sizeof(book));
  39. }
  40.  
  41. void operator<<(BinaryFiles& files, Book& book) {
  42.   files.GetOut().write((char*)&book, sizeof(book));
  43. }
  44.  
  45. int main() {
  46.   int n;
  47.   cout << "Enter number of books\n";
  48.   cin >> n;
  49.   BinaryFiles files1("file1.txt", "file1.txt");
  50.   BinaryFiles files2("file2.txt", "file2.txt");
  51.   for (int i = 0; i < n; ++i) {
  52.     Book book;
  53.     cout << "Enter name of book: ";
  54.     cin >> book.name;
  55.     cout << "Enter name of author: ";
  56.     cin >> book.author;
  57.     cout << "Enter year of book: ";
  58.     cin >> book.year;
  59.     files1 << book;
  60.   }
  61.   int year;
  62.   cout << "Enter some year: ";
  63.   cin >> year;
  64.   for (int i = 0; i < n; ++i) {
  65.     Book book;
  66.     files1 >> book;
  67.     if (book.year > year) {
  68. //      cout << book.name << " " << book.author << " " << book.year << "\n";
  69.         files2 << book;
  70.     }
  71.   }
  72.  
  73.   return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement