-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path9_C.cpp
More file actions
168 lines (148 loc) · 3.82 KB
/
9_C.cpp
File metadata and controls
168 lines (148 loc) · 3.82 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//Program to demonstrate usage of tellg() tellp() seekg() seekp().
#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();
int getRoll_no();
void setGrade(char);
};
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;
}
int student::getRoll_no(){
return roll_no;
}
void student::setGrade(char g){
/*
Objective : To update the existing grade of a student.
Input : New grade
*/
grade = g;
}
void write(ofstream &fout,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.
*/
fout.write((char*)&s,sizeof(s));
}
void read(ifstream &fin){
/*
Objective : To read the file 'student.dat'
Input : None.
Output : Data of file 'student.dat'
*/
student s;
cout<<"\n\n\t\t\tSTUDENT FILE\n\n";
while(fin.read((char*)&s,sizeof(s)))
s.showData();
}
int main()
{
ofstream fout;
ifstream fin;
fout.open("student_record.dat", ios::binary);
student s;
write(fout,student(1,"John",'B'));
write(fout,student(2,"Ron",'C'));
write(fout,student(3,"Harry",'F'));
write(fout,student(4,"Peter",'A'));
write(fout,student(5,"Bruce",'A'));
write(fout,student(6,"Taylor",'B'));
write(fout,student(7,"Daniel",'E'));
write(fout,student(8,"Jerry",'B'));
write(fout,student(9,"April",'B'));
write(fout,student(10,"Nata",'A'));
fout.close();
fin.open("student_record.dat", ios::binary);
cout<<fin.tellg(); //again set the reading pointer at beginning(for reading entire database)
cout<<"\n\n\t\t\tFILE CONTENTS:\n";
read(fin);
cout<<fin.tellg();
int rno;
long pos;
char g;
cout<<"\n\nEnter rollno of student whose record is to be modified: ";
cin>>rno;
fin.close();
fstream f;
f.open("student_record.dat", ios::binary|ios::in|ios::out);
while(!f.eof())
{
pos = f.tellg(); //storing the position of reading pointer which is at the beginning of current object being searched
f.read((char *)&s, sizeof(s));
if(s.getRoll_no() == rno )
{
cout<<"\nEnter new grade : ";
cin>>g;
s.setGrade(g);
f.seekp(pos); //updating the write pointer to point at beginning of the object with the matching roll number
f.write((char *)&s, sizeof(s));
break;
}
}
if(f.eof())
{
cout<<"\nRecord not found in the file..!!\n";
exit(1);
}
f.close();
fin.open("student_record.dat", ios::binary);
cout<<"\n\n\t\t\tNEW FILE CONTENTS:\n";
read(fin);
fin.close();
return 0;
}