-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
82 lines (65 loc) · 1.86 KB
/
test.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
std::vector<std::string> tokenise(std::string csvLine, char seperator)
{
std::vector<std::string> tokens;
signed int start, end;
std::string token;
start = csvLine.find_first_not_of(seperator, 0);
do
{
end = csvLine.find_first_of(seperator, start);
if (start == csvLine.length() || start == end) break; //nothing more to find
if (end >= 0) token = csvLine.substr(start, end - start); //we found the seperator
else token = csvLine.substr(start, csvLine.length() - start);
tokens.push_back(token);
start = end + 1;
}while(end != std::string::npos);
return tokens;
}
int main()
{
// std::string s = "thing,thing1,thing2";
// std::vector<std::string> tokens;
// tokens = tokenise(s, ',');
// for (std::string& t : tokens)
// {
// std::cout << t << std::endl;
// }
std::ifstream csvFile{"20231224.csv"};
std::string line;
if (csvFile.is_open())
{
std::cout << "File open" << std::endl;
std::vector<std::string> tokens;
while (std::getline(csvFile, line))
{
std::cout << line << std::endl;
tokens = tokenise(line, ',');
if (tokens.size() != 5)
{
std::cout << "Bad line" << std::endl;
continue;
}
//we have 5 tokens
try
{
double price = std::stod(tokens[3]);
double amount = std::stod(tokens[4]);
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
continue;
}
}
csvFile.close();
}
else
{
std::cout << "Could not open" << std::endl;
}
return 0;
}