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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
#include <iostream>
#include <fstream>
using namespace std;
const int FIELD_MAX_SIZE = 50;
const int BUFFER_LINE_SIZE = 1024;
struct OOPstudent
{
char firstName[FIELD_MAX_SIZE];
char lastName[FIELD_MAX_SIZE];
char email[FIELD_MAX_SIZE];
int fn = 0;
};
int countLines(const char* fileName)
{
ifstream f(fileName);
if (!f.is_open())
return -1;
int linesCount = 0;
while (!f.eof())
{
char buff[1024];
f.getline(buff, 1024);
linesCount++;
}
f.close();
return linesCount;
}
// abc,bcd,ab@Abv.bg,fn
void readStudent(const char* input, OOPstudent& student)
{
int ind = 0;
// first name
while (input[ind] != ',')
{
student.firstName[ind] = input[ind];
ind++;
}
student.firstName[ind] = '\0';
ind++;
//last name
int lastNameInd = 0;
while (input[ind] != ',')
student.lastName[lastNameInd++] = input[ind++];
student.lastName[lastNameInd] = '\0';
ind++;
//email
int emailInd = 0;
while (input[ind] != ',')
student.email[emailInd++] = input[ind++];
student.email[emailInd] = '\0';
ind++;
//fn
while (input[ind] != '\0')
(student.fn *= 10) += (input[ind++] - '0');
}
bool saveStudentsToFile(const OOPstudent* studentsCollection, int len, const char* fileName)
{
ofstream outFile(fileName);
if (!outFile.is_open())
return false;
outFile << "First name, Last name, email, fn" << endl;
for (int i = 0; i < len; i++)
outFile << studentsCollection[i].firstName << "," << studentsCollection[i].lastName << "," << studentsCollection[i].email << "," << studentsCollection[i].fn << endl;
outFile.close();
return true;
}
int indexOfFirstSpaceOrTerminatingZero(const char* command)
{
int ind = 0;
while (*command != ' ' && *command != '\t' && *command != '\0')
{
command++;
ind++;
}
return ind;
}
bool printStudent(const OOPstudent* arrayOfStudents, int len, int fn)
{
for (int i = 0; i < len; i++)
{
if (arrayOfStudents[i].fn == fn)
{
cout << arrayOfStudents[i].firstName << "," << arrayOfStudents[i].lastName << "," << arrayOfStudents[i].email << "," << arrayOfStudents[i].fn << endl;
return true;
}
}
return false;
}
bool editFirstName(OOPstudent* arrayOfStudents, int len, int fn, const char* newFirstName)
{
for (int i = 0; i < len; i++)
{
if (arrayOfStudents[i].fn == fn)
{
strcpy(arrayOfStudents[i].firstName, newFirstName);
return true; //success!
}
}
return false; //no such student
}
int main()
{
cout << "Open file:" << endl << ">";
char fileName[BUFFER_LINE_SIZE];
cin.getline(fileName, BUFFER_LINE_SIZE);
int studentsCount = countLines(fileName) - 1; // We ignore the first line
if (studentsCount <= 0)
{
cout << "Error while reading data from the file " << endl;
return -1;
}
OOPstudent* studentsCollection = new OOPstudent[studentsCount];
ifstream f(fileName, ios::_Nocreate);
if (!f.is_open())
{
cout << "Error!" << endl;
delete[] studentsCollection;
return -1;
}
int line = 1;
while (!f.eof())
{
char buff[BUFFER_LINE_SIZE];
f.getline(buff, BUFFER_LINE_SIZE);
if (line == 1)
{
line++;
continue;
}
readStudent(buff, studentsCollection[line - 2]);
line++;
}
f.close();
cout << "File successfully opened!" << endl;
char commandLine[BUFFER_LINE_SIZE];
do
{
cout << ">";
cin.getline(commandLine, 1024);
size_t len = strlen(commandLine);
//first argument (edit, save ...)
size_t firstArgSize = indexOfFirstSpaceOrTerminatingZero(commandLine);
char* commandFirstArg = new char[firstArgSize + 1];
for (int i = 0; i < firstArgSize; i++)
commandFirstArg[i] = commandLine[i];
commandFirstArg[firstArgSize] = '\0';
if (len == firstArgSize) //no more arguments - quit? Check is bellow
continue;
//second argument (students.csv, 45543 ...)
size_t secondArgSize = len - firstArgSize - 1;
char* commandSecondArg = new char[secondArgSize + 1];
for (int i = 0; i < secondArgSize; i++)
commandSecondArg[i] = commandLine[firstArgSize + 1 + i];
commandSecondArg[secondArgSize] = '\0';
if (strcmp(commandFirstArg, "print") == 0)
{
int fn = atoi(commandSecondArg);
bool res = printStudent(studentsCollection, studentsCount, fn);
if (!res)
cout << "Student with fn: " << fn << " doesn't exist!" << endl;;
}
else if (strcmp(commandFirstArg, "edit") == 0)
{
int fn = atoi(commandSecondArg);
//we have a third argument here. It's in the second arguments string
size_t seprator = indexOfFirstSpaceOrTerminatingZero(commandSecondArg);
size_t thirdArgumentSize = secondArgSize - seprator;
char* commandThirdArg = new char[thirdArgumentSize + 1];
for (int i = 0; i < thirdArgumentSize; i++)
commandThirdArg[i] = commandSecondArg[seprator + 1 + i];
commandThirdArg[thirdArgumentSize] = '\0';
bool result = editFirstName(studentsCollection, studentsCount, fn, commandThirdArg);
if (result)
cout << "Succesfully changed name!" << endl;
else
cout << "Student with fn: " << fn << "doesn't exist!" << endl;
delete[] commandThirdArg;
}
else if (strcmp(commandFirstArg, "save") == 0)
{
bool res = saveStudentsToFile(studentsCollection, studentsCount, commandSecondArg);
if (res)
cout << "Successfully saved to file: " << commandSecondArg << endl;
else
cout << "Error while saving in file: " << commandSecondArg << endl;
}
delete[] commandFirstArg;
delete[] commandSecondArg;
} while (strcmp(commandLine, "Quit"));
delete[] studentsCollection;
return 0;
}
|