codisinmyvines

aaaa

Apr 19th, 2020
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. struct Book
  6. {
  7.     char genre;
  8.     char author[30];
  9.     int page;
  10.     char title;
  11. };
  12. void createBINFILE(ifstream& f, ofstream& g)
  13. {  
  14.     Book x;
  15.     while (f>>x.genre>>x.author>>x.page>>x.title)
  16.     {
  17.         g.write((char*)&x, sizeof(Book));
  18.     }
  19. }
  20. void functionOtbor(ifstream& f)
  21. {
  22.     Book x;
  23.     int g; //1 - детектив //2 - фантастика //3- роман
  24.     cout << "Выберите жанр\n";
  25.     cout << "1 - детектив 2 - фантастика 3 - роман = ";
  26.     cin >> g;
  27.     switch (g)
  28.     {
  29.     case 1:
  30.         while (f.peek()!=EOF)
  31.         {  
  32.             f.read((char*)&x, sizeof(Book));
  33.             if (x.genre == 'd')
  34.             {
  35.                 cout << x.author << " " << x.page << "\n";
  36.             }
  37.         }
  38.         break;
  39.     case 2:
  40.         while (f.peek() != EOF)
  41.         {  
  42.             f.read((char*)&x, sizeof(Book));
  43.             if (x.genre == 'f')
  44.             {
  45.                 cout << x.author << " " << x.page << "\n";
  46.             }
  47.         }
  48.         break;
  49.     case 3:
  50.         while (f.peek() != EOF)
  51.         {  
  52.             f.read((char*)&x, sizeof(Book));
  53.             if (x.genre == 'n')
  54.             {
  55.                 cout << x.author << " " << x.page << "\n";
  56.             }
  57.         }
  58.         break;
  59.     }
  60. }
  61.  
  62. int main()
  63. {  
  64.     setlocale(LC_ALL, "RU");
  65.     ifstream intxt("book.txt");
  66.     ofstream outbin("booksbin.bin", ios::binary);
  67.     createBINFILE(intxt, outbin);
  68.     intxt.close();
  69.     outbin.close();
  70.     ifstream inbin("booksbin.bin", ios::binary);
  71.     functionOtbor(inbin);
  72.     inbin.close();
  73.  
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment