Advertisement
selebry

gdfsgdfgdg

Sep 28th, 2022
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. struct book {
  8. int isbn;
  9. char namebook[20];
  10. char author[30];
  11. unsigned int year_of_publication;
  12. };
  13. void createBinFile(string nametf,string namebf) {
  14. ifstream itf(nametf);
  15. if (!itf) return;
  16. fstream bf(namebf, ios::binary | ios::out);
  17. book b;
  18. while (!itf.eof()) {
  19. itf >> b.isbn;
  20. itf.get();
  21. itf.getline(b.namebook, 20);
  22. itf.getline(b.author, 30);
  23. itf >> b.year_of_publication;
  24. bf.write((char*)&b, sizeof(book));
  25. }
  26. itf.close();
  27. bf.close();
  28.  
  29. }
  30. void outBinFile(string namebf) {
  31. fstream bf(namebf, ios::binary | ios::in);
  32. if (!bf) return;
  33. book b;
  34. int len = sizeof(book);
  35. bf.read((char*)&b, len);
  36. while (!bf.eof()) {
  37. cout << b.isbn << ' ' << b.namebook << ' '<<b.author<<endl;
  38. bf.read((char*)&b, len);
  39. }
  40. bf.close();
  41. }
  42. int main()
  43. {
  44.  
  45. createBinFile("in.txt","test.bin");
  46. outBinFile("test.bin");
  47.  
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement