-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv.h
More file actions
37 lines (34 loc) · 740 Bytes
/
Copy pathcsv.h
File metadata and controls
37 lines (34 loc) · 740 Bytes
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
#include <fstream>
#include <sstream>
#include "grid.h"
using namespace std;
bool FilenameToArray(string filename, Grid& grid) {
ifstream ifs;
ifs.open(filename.c_str());
if (!ifs.is_open()) {
cout << "ERROR: Can't open file " << filename << endl;
return false;
}
string line, node;
stringstream ss;
int i = 0;
// csv to array strip newlines
while (getline(ifs, line)) {
int j = 0;
ss << line;
while (getline(ss, node, ',')) {
if (node[node.length() - 1] == '\r') {
node.resize(node.size() - 1);
}
if (grid.ok(i, j)) {
grid.set(i, j, atoi(node.c_str()));
j++;
} else {
return false;
}
}
i++;
ss.clear();
}
return true;
}