-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path9_B.cpp
More file actions
141 lines (117 loc) · 2.84 KB
/
9_B.cpp
File metadata and controls
141 lines (117 loc) · 2.84 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//Program to write and read object using read and write function.
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<fstream>
using namespace std;
class student
{
/*
Objective : Class definition for student.
Description : Data Members : Roll number of a student, Name of the student, Grade of the student.
Member Functions : getDate() -> Take input for data members from the user
showData() -> Displays the value of data members.
*/
private:
int roll_no;
char name[30];
char grade;
public:
student();
student(int,char[],char);
void getData();
void showData();
};
student::student(){
//Default Constructor
roll_no = 0;
strcpy(name,"");
grade = 'X';
}
student::student(int r, char n[], char g){
/*
Objective : Contructor to initialize the values during the declaration of the object.
*/
roll_no = r;
strcpy(name,n);
grade = g;
}
void student::getData(){
/*
Objective : To take input from user and initialize the data menmbers.
Input : None.
Output : None.
Side Effect : Values of the object is updated with the input values
*/
cout<<"\n\n\t\tEnter student's details\n";
cout<<"Enter roll number: ";
cin>>roll_no;
cout<<"Enter name:";
cin.ignore();
cin.getline(name,30);
cout<<"Enter Grade:";
cin>>grade;
}
void student::showData(){
/*
Objective : To Display the members of the object on output console.
*/
cout<<"Roll number : "<<roll_no<<"\tName : "<<name<<"\tGrade : "<<grade<<endl;
}
void write(student s){
/*
Objective : To write an object of class student into file.
Input : s -> object of class student.
Return value : None.
Side Effect : The object passed is appended at the end of the file.
*/
ofstream fout;
fout.open("student.dat",ios::binary|ios::app); //append mode
if(!fout){
cout<<"Error in creating file.."<<endl;
exit(1);
}
fout.write((char*)&s,sizeof(s));
fout.close();
}
void read(){
/*
Objective : To read the file 'student.dat'
Input : None.
Output : Data of file 'student.dat'
*/
ifstream fin;
fin.open("student.dat",ios::binary);
if(!fin){
cout<<"Error in opening file..";
exit(1);
}
student s;
cout<<"\n\n\t\t\tSTUDENT FILE\n\n";
while(fin.read((char*)&s,sizeof(s)))
s.showData();
fin.close();
}
int main(){
/*
Objective : Driver function to demonstrate reading and writing object from/to a binary file.
*/
student s;
int choice;
while(true){
cout<<"\n\n\t\t\tMENU\n\n1. WRITE\n2. READ\n3. EXIT\n\nEnter your choice : ";
cin>>choice;
switch(choice){
case 1: s.getData();
write(s);
break;
case 2: read();
break;
case 3: exit(0);
break;
default: cout<<"\n\nINVALID CHOICE!!";
break;
}
}
return 0;
}