Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <iostream>
- #include <fstream>
- using namespace std;
- struct book {
- int isbn;
- char namebook[20];
- char author[30];
- unsigned int year_of_publication;
- };
- void createBinFile(string nametf,string namebf) {
- ifstream itf(nametf);
- if (!itf) return;
- fstream bf(namebf, ios::binary | ios::out);
- book b;
- while (!itf.eof()) {
- itf >> b.isbn;
- itf.get();
- itf.getline(b.namebook, 20);
- itf.getline(b.author, 30);
- itf >> b.year_of_publication;
- bf.write((char*)&b, sizeof(book));
- }
- itf.close();
- bf.close();
- }
- void outBinFile(string namebf) {
- fstream bf(namebf, ios::binary | ios::in);
- if (!bf) return;
- book b;
- int len = sizeof(book);
- bf.read((char*)&b, len);
- while (!bf.eof()) {
- cout << b.isbn << ' ' << b.namebook << ' '<<b.author<<endl;
- bf.read((char*)&b, len);
- }
- bf.close();
- }
- int main()
- {
- createBinFile("in.txt","test.bin");
- outBinFile("test.bin");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement