Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 02 / studentsDatabase.cpp
diff options
context:
space:
mode:
Commit51f517bf92ce0aa616de37dd73074ce3e3579875 (patch)
Author
 Angeld55 <angel_dimitriev@abv.bg>
2021-03-05 14:39:17 +0200
Committer
 Angeld55 <angel_dimitriev@abv.bg>
2021-03-05 14:39:17 +0200
Files7ebbc2e009dbded38e10eae4a652a15596010a29 /Sem. 02/studentsDatabase.cpp
Object-oriented_programming_FMI-51f517bf92ce0aa616de37dd73074ce3e3579875.tar
Object-oriented_programming_FMI-51f517bf92ce0aa616de37dd73074ce3e3579875.tar.gz
Object-oriented_programming_FMI-51f517bf92ce0aa616de37dd73074ce3e3579875.zip
Parent1f500f0eff09b834d7f863282874038213e4c329 (diff)
sem 2
1 files changed, 124 insertions, 0 deletions
-rw-r--r--Sem. 02/studentsDatabase.cpp124
diff --git a/Sem. 02/studentsDatabase.cpp b/Sem. 02/studentsDatabase.cpp
new file mode 100644
index 0000000..ba3e420
--- /dev/null
+++ b/Sem. 02/studentsDatabase.cpp
@@ -0,0 +1,124 @@
+#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;
+
+};
+
+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';
+
+ //fn
+ //??
+}
+
+void saveStudentsToFile(const OOPstudent* studentsCollection,int len, const char* fileName)
+{
+ ofstream outFile(fileName);
+
+ if (!outFile.is_open())
+ return;
+
+ for (int i = 0; i < len; i++)
+ outFile << studentsCollection[i].firstName << " " << studentsCollection[i].lastName << " " << studentsCollection[i].email << endl;
+
+ outFile.close();
+}
+
+int main()
+{
+
+ int studentsCount = countLines("students.csv") - 1; // We ignore the first line
+
+ OOPstudent* studentsCollection = new OOPstudent[studentsCount];
+
+ ifstream f("students.csv");
+
+ 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();
+
+
+ //Do stuff
+
+ saveStudentsToFile(studentsCollection, studentsCount, "result.txt");
+
+
+
+
+ delete[] studentsCollection;
+ return 0;
+} \ No newline at end of file