Advertisement
Proff_Ust

Books

Jun 11th, 2019
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <clocale>
  3. #include <fstream>
  4. #define BOOKS_NUM 3
  5. using namespace std;
  6.  
  7. struct tBook
  8. {
  9.     char* author;
  10.     char* name;
  11.     int year;
  12.     char* spec;
  13. };
  14.  
  15. tBook* inputBook()
  16. {
  17.     tBook* temp = new tBook;
  18.     cout<<"Введите автора книги ";
  19.     temp->author = new char[40];
  20.     cin>>temp->author;
  21.     cout<<"Введите наименование книги ";
  22.     temp->name = new char[80];
  23.     cin>>temp->name;
  24.     cout<<"Введите год издания книги ";
  25.     cin>>temp->year;
  26.     cout<<"Введите специальность книги ";
  27.     temp->spec = new char[40];
  28.     cin>>temp->spec;
  29.     return temp;
  30. }
  31.  
  32. int outputBook(tBook* book)
  33. {
  34.     cout<<"Автор книги: "<<book->author<<endl;
  35.     cout<<"Наименование книги: "<<book->name<<endl;
  36.     cout<<"Год издания: "<<book->year<<endl;
  37.     cout<<"Специальность: "<<book->spec<<endl;
  38.     return 0;
  39. }
  40.  
  41. tBook* readBookFromFile(ifstream& inFile)
  42. {
  43.     tBook* temp = new tBook;
  44.     temp->author = new char[40];
  45.     temp->name = new char[80];
  46.     temp->spec = new char[40];
  47.     inFile>>temp->author;
  48.     inFile>>temp->name;
  49.     inFile>>temp->year;
  50.     inFile>>temp->spec;
  51.     return temp;
  52. }
  53.  
  54. int writeBookToFile(tBook* book, ofstream& outFile)
  55. {
  56.     outFile<<book->author<<endl;
  57.     outFile<<book->name<<endl;
  58.     outFile<<book->year<<endl;
  59.     outFile<<book->spec<<endl;
  60.     return 0;
  61. }
  62.  
  63. int task(ifstream inFile)
  64. {
  65.     return 0;
  66. }
  67.  
  68. int main()
  69. {
  70.     setlocale(LC_ALL,"rus");
  71.     ofstream outputFile("test.txt");
  72.     for(int i=1;i<=BOOKS_NUM;i++)
  73.     {
  74.         cout<<"Введите данные об "<<i<<"-ой книге"<<endl;
  75.         writeBookToFile(inputBook(), outputFile);
  76.     }
  77.     outputFile.close();
  78.  
  79.     ifstream inputFile("test.txt");
  80.     for(int i=1;i<=BOOKS_NUM;i++)
  81.     {
  82.         cout<<"Данные об "<<i<<"-ой книге"<<endl;
  83.         outputBook(readBookFromFile(inputFile));
  84.         cout<<endl;
  85.     }
  86.     inputFile.close();
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement