-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvreader.cpp
More file actions
43 lines (36 loc) · 827 Bytes
/
csvreader.cpp
File metadata and controls
43 lines (36 loc) · 827 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
38
39
40
41
42
43
#include "csvreader.h"
using namespace std;
CSVReader::CSVReader()
{
}
void CSVReader::load(Graph* graph, string filename)
{
fstream input;
input.open(filename.c_str());
if(input)
{
string record;
getline(input, record);
while(input)
{
addRecordToGraph(graph, record);
getline(input, record);
}
}
else
{
cout << "Could not open the CSV file for reading." << endl;
throw CSVReadException();
}
input.close();
}
void CSVReader::addRecordToGraph(Graph* graph, string record)
{
// Split the record up according to commas and convert each field to float
char* record_c = new char[record.length() + 1];
Pair data;
strcpy(record_c, record.c_str());
data.x = atof(strtok(record_c, ","));
data.y = atof(strtok(NULL, ","));
graph->addData(data);
}