Website Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commitec8585225f00408a245ad3422849479e6dda6c10 (patch)
Author
 Angel Dimitriev <achi_d@abv.bg>
2021-03-15 12:07:52 +0200
Committer
 GitHub <noreply@github.com>
2021-03-15 12:07:52 +0200
Files66ca3a6e34f3cebc7a04558fe4fab450a4eedd84
Object-oriented_programming_FMI-ec8585225f00408a245ad3422849479e6dda6c10.tar
Object-oriented_programming_FMI-ec8585225f00408a245ad3422849479e6dda6c10.tar.gz
Object-oriented_programming_FMI-ec8585225f00408a245ad3422849479e6dda6c10.zip
Parent066ad453f1de3f755c3950207c492645e0411367 (diff)
Update studentsDatabase.cpp
1 files changed, 141 insertions, 12 deletions
-rw-r--r--Sem. 02/studentsDatabase.cpp153
diff --git a/Sem. 02/studentsDatabase.cpp b/Sem. 02/studentsDatabase.cpp
index ba3e420..7f9a5f5 100644
--- a/Sem. 02/studentsDatabase.cpp
+++ b/Sem. 02/studentsDatabase.cpp
@@ -11,7 +11,7 @@ struct OOPstudent
char firstName[FIELD_MAX_SIZE];
char lastName[FIELD_MAX_SIZE];
char email[FIELD_MAX_SIZE];
- int fn;
+ int fn = 0;
};
@@ -31,7 +31,7 @@ int countLines(const char* fileName)
f.close();
return linesCount;
-
+
}
// abc,bcd,ab@Abv.bg,fn
void readStudent(const char* input, OOPstudent& student)
@@ -60,32 +60,88 @@ void readStudent(const char* input, OOPstudent& student)
while (input[ind] != ',')
student.email[emailInd++] = input[ind++];
student.email[emailInd] = '\0';
+ ind++;
//fn
- //??
+ while (input[ind] != '\0')
+ (student.fn *= 10) += (input[ind++] - '0');
+
}
-void saveStudentsToFile(const OOPstudent* studentsCollection,int len, const char* fileName)
+bool saveStudentsToFile(const OOPstudent* studentsCollection, int len, const char* fileName)
{
ofstream outFile(fileName);
if (!outFile.is_open())
- return;
+ 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 << endl;
+ 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()
{
- int studentsCount = countLines("students.csv") - 1; // We ignore the first line
+ 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];
- OOPstudent* studentsCollection = new OOPstudent[studentsCount];
- ifstream f("students.csv");
+ ifstream f(fileName, ios::_Nocreate);
if (!f.is_open())
{
@@ -111,14 +167,87 @@ int main()
}
f.close();
+ cout << "File successfully opened!" << endl;
+
+ char commandLine[BUFFER_LINE_SIZE];
+ do
+ {
+ cout << ">";
+ cin.getline(commandLine, 1024);
+ size_t len = strlen(commandLine);
+
+
- //Do stuff
+ //first argument (edit, save ...)
+ size_t firstArgSize = indexOfFirstSpaceOrTerminatingZero(commandLine);
+ char* commandFirstArg = new char[firstArgSize + 1];
- saveStudentsToFile(studentsCollection, studentsCount, "result.txt");
+ 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;
-} \ No newline at end of file
+}