-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathread_csv.h
52 lines (42 loc) · 1.04 KB
/
read_csv.h
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <stdexcept>
#include "structures/transaction.h"
using namespace std;
bool read_csv(std::string filename, vector<Transaction>& transacts)
{
string line, word;
ifstream file(filename);
if(!file.is_open())
{
return false;
}
while (getline(file, line))
{
stringstream currline(line);
std::string to, from, moment_s, ammount_s;
getline(currline,to,',');
getline(currline,from,',');
getline(currline,moment_s,',');
getline(currline,ammount_s,',');
try
{
long int moment = atoi(moment_s.c_str());
long int ammount = atoi(ammount_s.c_str());
Transaction t;
t.from = from;
t.to = to;
t.moment = moment;
t.ammount = ammount;
transacts.push_back(t);
}
catch(std::invalid_argument a())
{
return false;
}
}
return true;
}