-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_from_csv.cpp
68 lines (59 loc) · 1.51 KB
/
reader_from_csv.cpp
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
#include "reader_from_csv_.h"
#include "user_.h"
/*
* Class for reading from a file in CSV format
*/
Reader::Reader(const string& filename) : filename(filename) {}
/*
* This method opens the file if it is closed.
*/
void Reader::openReadFile() {
if (!file.is_open()) {
file.open(filename);
}
}
/*
* This method closes the file if it is open.
*/
void Reader::closeReadFile() {
if (file.is_open()) {
file.close();
}
}
/*
* This method reads each value separated by a comma from the file.
* It returns a vector of User objects to create users based on each vector element later on.
* The sstream library is used to read from the file.
*
* First, each value is read and stored in temporary variables.
* Then, the data from the temporary variables is used to create a User object.
* These User objects are stored in a dynamic vector.
*/
vector<User> Reader::readLines() {
vector<User> users_from_read;
string line = "";
while (getline(file, line)) {
stringstream inputString(line);
string name;
string surname;
int balance;
int id;
string tempString;
getline(inputString, name, ',');
getline(inputString, surname, ',');
getline(inputString, tempString, ',');
balance = atoi(tempString.c_str());
getline(inputString, tempString, ',');
id = atoi(tempString.c_str());
User user(id, name, surname, balance);
users_from_read.push_back(user);
//user.getUser();
line = "";
}
return users_from_read;
}
void Reader::displayUsers(vector<User>& users) {
for (auto user : users) {
user.getUser();
}
}