Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 02 / studentsDatabase.cpp
blob: ba3e42013d40681b411751726469a5770d737840 (plain) (blame)
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
#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;
}