-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprimaryIndex.cpp
More file actions
236 lines (197 loc) · 5.02 KB
/
Copy pathprimaryIndex.cpp
File metadata and controls
236 lines (197 loc) · 5.02 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
227
228
229
230
231
232
233
234
235
236
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
class Student
{
public:
char fname[20], lname[20], usn[10], sem[5], branch[5];
};
Student students[20];
Student s;
fstream f;
fstream fi;
string filename = "students.txt";
string idxfile = "index.txt";
int numRecords = 0;
int recordSize = 70;
char buffer[100];
const char *delimeter = "|";
const char *padding = "*";
// FUNCTIONS
// Write student record to file
void pack(Student &student)
{
f.open(filename, ios::out | ios::app);
const char *RRN = to_string(numRecords).c_str();
strcpy(buffer, student.fname);
strcat(buffer, delimeter);
strcat(buffer, student.lname);
strcat(buffer, delimeter);
strcat(buffer, student.usn);
strcat(buffer, delimeter);
strcat(buffer, student.sem);
strcat(buffer, delimeter);
strcat(buffer, student.branch);
strcat(buffer, delimeter);
for (int i = strlen(buffer); i < recordSize; i++)
strcat(buffer, padding);
f << buffer << endl; // Write record to file
f.close();
fi.open(idxfile, ios::out | ios::app);
fi << student.usn << delimeter << numRecords << delimeter << endl;
fi.close();
}
// Read student data and return Student object
void read(Student &student)
{
cout << "Enter the fname: ";
cin >> student.fname;
cout << "Enter the lname: ";
cin >> student.lname;
cout << "Enter the USN: ";
cin >> student.usn;
cout << "Enter the sem: ";
cin >> student.sem;
cout << "Enter the branch: ";
cin >> student.branch;
}
// Display student data
void display(Student &student)
{
cout << student.fname << "\t";
cout << student.lname << "\t";
cout << student.usn << "\t";
cout << student.sem << "\t";
cout << student.branch << endl;
}
void displayAll()
{
if (numRecords == 0)
{
cout << "No records found!" << endl;
return;
}
cout << "\nStudent records are:" << endl;
cout << "Fname\tLname\tUSN\tSem\tBranch" << endl;
for (int i = 0; i < numRecords; i++)
display(students[i]);
}
// Unpack student records from file
void unpack()
{
f.open(filename, ios::in);
for (int i = 0; i < numRecords; i++)
{
f.getline(buffer, 100);
sscanf(buffer,
"%[^|]|%[^|]|%[^|]|%[^|]|%[^|]|", // Read all 5 fields using RE
students[i].fname,
students[i].lname,
students[i].usn,
students[i].sem,
students[i].branch);
}
f.close();
}
int search()
{
char usn[10];
cin >> usn;
char tempusn[10], idx[5];
fi.open(idxfile, ios::in);
Student student;
for (int i = 0; i < numRecords; i++)
{
fi.getline(buffer, 30);
sscanf(buffer, "%[^|]|%[^|]|", tempusn, idx);
if (strcmp(usn, tempusn) == 0)
{
int seekIdx = atoi(idx); // Ascii to int
int seekPosition = recordSize * (seekIdx - 1);
seekPosition += (seekIdx - 1) * 2; // This is needed to account for crlf in windows
// Seek to the right position
f.open(filename, ios::in);
f.seekg(seekPosition, ios::beg);
f.read(buffer, recordSize);
sscanf(buffer,
"%[^|]|%[^|]|%[^|]|%[^|]|%[^|]|", // Read all 5 fields using RE
student.fname,
student.lname,
student.usn,
student.sem,
student.branch);
cout << "Match Found!" << endl;
display(student);
f.close();
fi.close();
return seekIdx;
}
}
f.close();
fi.close();
cout << "Match not found!" << endl;
return -1;
}
void deleteRecord(int index)
{
if (index == -1)
return;
unpack();
remove(filename.c_str());
remove(idxfile.c_str());
f.open(filename, ios::out);
fi.open(idxfile, ios::out);
f.close();
fi.close();
int oldCount = numRecords;
numRecords = 0;
for (int i = 0; i < oldCount; i++)
{
if (i + 1 != index)
{
numRecords++;
pack(students[i]);
}
}
cout << "Record has been deleted!" << endl;
}
int main()
{
f.open(filename, ios::out);
fi.open(idxfile, ios::out);
f.close();
fi.close();
char choice;
while (1)
{
cout << "\n1.Pack\n2.Unpack\n3.Search\n4.Delete\n5.Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case '1':
numRecords++;
read(s);
pack(s);
break;
case '2':
unpack();
displayAll();
break;
case '3':
cout << "Enter USN to search: ";
search();
break;
case '4':
cout << "Enter USN to delete: ";
deleteRecord(search());
break;
case '5':
return 0;
default:
cout << "\nInvalid choice!" << endl;
}
}
return 0;
}