-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
97 lines (56 loc) · 1.64 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// tester for tokenising
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
std::vector<std::string> tokenise(std::string csvLine, char separator)
{
// stores the tokens
std::vector<std::string> tokens;
// delineate position
signed int start, end;
std::string token;
start = csvLine.find_first_not_of(separator, 0);
do {
end = csvLine.find_first_of(separator, start);
if (start == csvLine.length() || start == end) break;
if (end >= 0) token = csvLine.substr(start, end - start);
else token = csvLine.substr(start, csvLine.length() - start);
tokens.push_back(token);
start = end+1;
} while(end != std::string::npos);
return tokens;
}
int main()
{
// testing
// std::vector<std::string> tokens;
// std::string s = "thing1,thing2,thing3";
// tokens = tokenise(s, ',');
// for (std::string& t: tokens)
// {
// std::cout << t << std::endl;
// }
std::ifstream csvFile{"data.csv"};
std::string line;
std::vector<std::string> tokens;
if (csvFile.is_open())
{
std::cout << "File open" << std::endl;
while (std::getline(csvFile, line))
{
std::cout << "read line " << line << std::endl;
tokens = tokenise(line, ',');
for (std::string& t : tokens)
{
std::cout << t << std::endl;
}
}
}
else
{
std::cout << "Could not open file" << std::endl;
}
csvFile.close();
return 0;
}