-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsecsv.cpp
50 lines (40 loc) · 1.11 KB
/
parsecsv.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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
/* std::vector<std::string> getNextLineAndSplitIntoTokens()
{
std::vector<std::string> result;
std::string line;
std::getline(str,line);
std::stringstream lineStream(line);
std::string cell;
while(std::getline(lineStream,cell, ','))
{
result.push_back(cell);
}
// This checks for a trailing comma with no data after it.
if (!lineStream && cell.empty())
{
// If there was a trailing comma then add an empty element.
result.push_back("");
}
return result;
} */
int main (){
std::ifstream file;
file.open("testfile.txt");
if(!file) { // file couldn't be opened
std::cout << "Error: file could not be opened" << std::endl;
return 1;
}
std::string str;
std::vector<int> result;
while(std::getline(file, str, ','))
{
result.push_back(stoi(str));
//std::cout << "The next number is " << stoi(str) << std::endl;
}
std::cout << result[1] << std::endl;
return 0;
}