-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserialization.cpp
More file actions
53 lines (41 loc) · 810 Bytes
/
serialization.cpp
File metadata and controls
53 lines (41 loc) · 810 Bytes
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
50
51
52
#include<iostream>
#include<fstream>
using namespace std;
class Student
{
public:
int roll = 0;
string name;
string branch;
friend ofstream & operator <<(ofstream & ofs, Student & s);
friend ifstream & operator >>(ifstream & ifs, Student & s);
};
ofstream & operator <<(ofstream & ofs, Student &s)
{
ofs<<s.name<<endl;
ofs<<s.roll<<endl;
ofs<<s.branch<<endl;
return ofs;
}
ifstream & operator >> (ifstream &ifs, Student & s)
{
ifs>>s.name>>s.roll>>s.branch;
return ifs;
}
int main()
{
Student s1;
ofstream ofs("student.txt", ios::trunc);
s1.name="Swapnil";
s1.roll = 100;
s1.branch="Computer";
ofs<<s1;
ofs.close();
ifstream ifs("student.txt");
ifs>>s1;
cout<<"Name "<<s1.name<<endl;
cout<<"Roll "<<s1.roll<<endl;
cout<<"Branch "<<s1.branch<<endl;
ifs.close();
return 0;
}