-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbinaryclass.cpp
49 lines (49 loc) · 930 Bytes
/
binaryclass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream>
#include<fstream>
using namespace std;
class student
{
int roll;
string name;
public:
student()
{
roll=41;
name="Nishkarsh Raj Khare";
}
void insert()
{
cout<<"Lets insert the value"<<endl;
cout<<"Enter the name of the student"<<endl;
getline(cin,name);
cout<<"Enter the roll number of the student"<<endl;
cin>>roll;
}
void show()
{
cout<<"Name of the student is "<<name<<endl;
cout<<"Roll number of the student is "<<roll<<endl;
}
};
int main()
{
cout<<"Binary files with classes"<<endl;
fstream nish1;
nish1.open("classbin.txt",ios::binary|ios::app);
student s1;
s1.insert();
nish1.write((char*)&s1,sizeof(s1));
cout<<"We have written the contents inside the file"<<endl<<"Lets Read it Maaaaaaaaaaaaan"<<endl;
student s2;
nish1.close();
fstream nish2;
int i;
nish2.open("classbin.txt",ios::binary|ios::in);
for(i=0;i<2;i++)
{
nish2.read((char*)&s2,sizeof(s2));
s2.show();
}
cout<<"Goodbye"<<endl;
return 0;
}