-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp12.cpp
More file actions
197 lines (175 loc) · 4.83 KB
/
Copy pathp12.cpp
File metadata and controls
197 lines (175 loc) · 4.83 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
#include <cstring>
#include <fstream>
#include <iostream>
using namespace std;
typedef struct student {
int rollNo;
char name[50];
char div;
char address[100];
} student;
class studentDatabase {
string fileName = "student_data.dat";
public:
studentDatabase() {
fstream fileObj(fileName);
if (fileObj.fail()) {
fileObj.open(fileName, ios::out);
cout << "New file created." << endl;
} else {
cout << "File already exists." << endl;
}
fileObj.close();
}
void addStudent();
void searchStudent();
void deleteStudent();
void displayAll();
};
void studentDatabase::searchStudent() {
int roll;
student s;
bool status = false;
// take input of roll number to delete
cout << "Roll number to search:\t";
cin >> roll;
// opening files to delete a record
ifstream readFile;
readFile.open(fileName, ios::in | ios::binary);
// looking for record
while (readFile.read((char*)&s, sizeof(student))) {
if (s.rollNo == roll) {
status = true;
break;
}
}
readFile.close();
if (status) {
cout << "--- RECORD FOUND ---" << endl;
cout << "Roll number:\t" << s.rollNo << endl;
cout << "Name:\t" << s.name << endl;
cout << "Division:\t" << s.div << endl;
cout << "Address:\t" << s.address << endl;
cout << "--- END OF RECORD ---" << endl;
} else {
cout << "Record not found." << endl;
}
}
void studentDatabase::deleteStudent() {
int roll;
student s;
bool status = false;
// take input of roll number to delete
cout << "Roll number to delete:\t";
cin >> roll;
// opening files to delete a record
ifstream readFile;
readFile.open(fileName, ios::in | ios::binary);
ofstream writeFile;
writeFile.open("~" + fileName, ios::out | ios::binary);
writeFile.clear();
// looking for record
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();
// moving temp file back to original file
if (status) {
readFile.open("~" + fileName, ios::in | ios::binary);
writeFile.open(fileName, ios::out | ios::binary);
writeFile.clear();
writeFile << readFile.rdbuf();
readFile.close();
writeFile.close();
// remove("~"+fileName);
cout << "Record deleted." << endl;
} else {
cout << "Record not found." << endl;
}
}
void studentDatabase::addStudent() {
student s;
cout << "Roll number:\t";
cin >> s.rollNo;
cout << "Name:\t";
cin.ignore();
cin.getline(s.name, 50);
cout << "Division:\t";
// cin.ignore();
cin >> s.div;
cout << "Address:\t";
cin.ignore();
cin.getline(s.address, 100);
// cin.ignore();
ofstream file(fileName, ios::out | ios::binary | ios::app);
// file.seekp(ios::end);
file.write((char*)&s, sizeof(student)) << flush;
if (file.fail()) {
cout << "Failed to add student record." << endl;
} else {
cout << "Student added successfully." << endl;
}
file.close();
}
void studentDatabase::displayAll() {
ifstream file;
student s;
int count = 0;
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.address << endl;
}
if (count == 0) {
cout << "No records found." << endl;
}
file.close();
}
int main() {
int ch;
studentDatabase db;
// loop
do {
cout << endl;
cout << "--- MAIN MENU ---" << endl;
cout << "1 -> Add record" << endl;
cout << "2 -> Delete record" << endl;
cout << "3 -> Search record" << endl;
cout << "4 -> Display all records" << endl;
cout << "0 -> Exit" << endl << endl;
cout << "Choose an option (0-4):\t";
cin >> ch;
switch (ch) {
case 0:
cout << "\n\n// END OF CODE\n\n" << endl;
break;
case 1:
db.addStudent();
break;
case 2:
db.deleteStudent();
break;
case 3:
db.searchStudent();
break;
case 4:
cout << "All records are:\t" << endl;
db.displayAll();
break;
default:
cout << "Please choose a valid option (0-4):\t" << endl;
break;
}
} while (ch != 0);
return 0;
}
// END OF CODE