Writing to Binary file in C++ program

Words
104
Reading
1 min
Listen
Play
9y


Program Body

#include<iostream>
#include <fstream>
#include <string>
using namespace std;

class vamp{
private:
    char by[500];
    int HowMany;
public:
void input(){
     cout<<"who transformed the humans into vampires today and how many"<<endl;
gets(by);
cin>>HowMany;
 }
 void show(){
 cout<<"Mr."<<by<<" transformed "<<HowMany<<" humans into vampires"<<endl;
 }
 };
int main(){
vamp trans;
trans.input();
fstream file("vamps.bin",ios::binary|ios::in|ios::out|ios::app);
if(!file.is_open()){cout<<"error wihle loading file"<<endl;
}else{
    file.write((char*)&trans,sizeof (trans));
    trans.show();
    file.close();
}
}

Working of program

Note

The binary file can't be read by humans it is specially made for understanding of computers.

main difference between text file and binary file is how they handle the end lines and gaps.

I hope you guys like it.

Writing to Binary file in C++ program | Ecency