Repository navigation
Expand file tree
/
Copy pathf23.cpp
More file actions
226 lines (175 loc) · 5.04 KB
/
Copy pathf23.cpp
File metadata and controls
226 lines (175 loc) · 5.04 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* Department maintains a student information. The file contains roll number, name, division, and address. Allow user to add delete information
of student. Display information of particular student. If record of student does not exist an appropriate message is displayed. If it is, then
the system displays the student details. Use sequential file to maintain the data. */
#include <iostream>
#include <fstream>
using namespace std;
typedef struct student {
int rollno;
char name[50];
char div;
char add[50];
} student;
class Database {
public:
string filename = "student_data.dat";
Database() {
fstream fileobj(filename); // 'fileobj' object opens 'filename' file
if(fileobj.fail()) {
fileobj.open(filename, ios :: out);
cout << "\nNew file created." << endl;
}
else {
cout << "\nFile already exists." << endl;
}
fileobj.close();
}
void add();
void del();
void display();
void search();
};
void Database :: add() {
student s;
cout << "\nEnter roll no. of the student: ";
cin >> s.rollno;
cout << "\nEnter the name of the student: ";
cin.ignore();
cin.getline(s.name, 50);
cout << "\nEnter division: ";
cin >> s.div;
cout << "\nEnter the address: ";
cin.ignore();
cin.getline(s.add, 100);
ofstream file(filename, ios :: out | ios :: binary | ios :: app);
file.write((char*) &s, sizeof(student)) << flush;
if(file.fail()) {
cout << "\nFailed to add record." << endl;
}
else {
cout << "\nRecord added successfully." << endl;
}
file.close();
}
void Database :: del() {
student s;
int roll;
bool status = false;
cout << "\nEnter roll no. to be deleted: ";
cin >> roll;
// opens the database file for reading in binary mode
ifstream readfile;
readfile.open(filename, ios :: in | ios :: binary);
// opens a temporary file for writing to store records other than the one to be deleted
ofstream writefile;
writefile.open("~" + filename, ios :: out | ios :: binary);
writefile.clear();
while(readfile.read((char*) &s, sizeof(student))) {
if(s.rollno == roll) {
status = true;
}
else {
writefile.write((char*) &s, sizeof(student)) << flush;
}
}
readfile.close();
writefile.close();
if(status){
readfile.open("~" + filename, ios :: in | ios :: binary);
writefile.open(filename, ios :: out | ios :: binary);
writefile.clear();
// copies contents of the temporary file into the database file
writefile << readfile.rdbuf();
readfile.close();
writefile.close();
cout << "\nRecord deleted." << endl;
}
else {
cout << "\nRecord not found." << endl;
}
}
void Database :: display() {
student s;
int count = 0;
ifstream file;
file.open(filename, ios :: in | ios :: binary);
while(file.read((char*) &s, sizeof(student))) {
count++;
cout << count << ". ";
cout << s.rollno << " | ";
cout << s.name << " | ";
cout << s.div << " | ";
cout << s.add << endl;
}
if(count == 0) {
cout << "\nNo records found." << endl;
}
file.close();
}
void Database :: search() {
student s;
int roll;
bool status = false;
cout << "\nEnter roll number to be searched: ";
cin >> roll;
ifstream readfile;
readfile.open(filename, ios :: in | ios :: binary);
while(readfile.read((char*) &s, sizeof(student))) {
if(s.rollno == roll){
status = true;
break;
}
}
readfile.close();
if(status) {
cout << "\nFile record with details: " << endl;
cout << "Roll no.: " << s.rollno << endl;
cout << "Name: " << s.name << endl;
cout << "Div: " << s.div << endl;
cout << "Address: " << s.add << endl;
}
else {
cout << "\nNo record found." << endl;
}
}
int main() {
Database obj;
int a, ch;
char ans;
cout << "\n\n*** MENU ***\n";
cout << "1. Add record" << endl;
cout << "2. Delete record" << endl;
cout << "3. Search record" << endl;
cout << "4. Display all records" << endl;
a = 1;
while(a == 1) {
cout << "\nEnter choice: ";
cin >> ch;
switch(ch) {
case 1:
obj.add();
break;
case 2:
obj.del();
break;
case 3:
obj.search();
break;
case 4:
obj.display();
break;
default:
cout << "\nInvalid choice." << endl;
break;
}
cout << "\nWould you like to choose again? (y/n): ";
cin >> ans;
if(ans == 'y') {
a = 1;
}
else {
a = 0;
cout << "\nThank you!\n";
}
}
}