-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSV_Parser.cpp
68 lines (55 loc) · 1.35 KB
/
CSV_Parser.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<bits/stdc++.h>
using namespace std;
vector<vector<string>> parse_csv(const string&filename) //parses data from csv
{
ifstream file(filename);
if (!file.is_open()) {
cerr << "Error opening " << filename << endl;
return {};
}
/* Handling the 3-byte Byte Order Mark (BOM -> 0xEF 0xBB 0xBF)
character sequence (UTF-8 CSV format) */
char BOM[3];
file.read(BOM,3);
if(!(BOM[0] == '\xEF' && BOM[1] == '\xBB' && BOM[2] == '\xBF'))
{
file.seekg(0);
}
vector<vector<string>>data;
vector<string>row;
string line,cell;
while(getline(file,line))
{
row.clear();
istringstream lineStream(line);
while(getline(lineStream,cell,','))
{
row.push_back(cell);
}
data.push_back(row);
}
file.close();
return data;
}
void show_data(vector<vector<string>>&data)
{
for(auto&row:data)
{
bool first=true;
for(auto&field:row)
{
if(!first)
{
cout<<" | ";
}
cout<<field;
first= false; // printing the full row
}
cout<<endl;
}
}
/*int main()
{
vector<vector<string>>data = parse_csv("courses_data.csv");
std::cout << "Current working directory: " << std::filesystem::current_path() << std::endl;
}*/