Columns from CSV to vectors

Hello,

I am attempting to read a CSV file containing three columns of the format string, string, double. First of all I cannot get the file to load properly so I cannot tell if my storing in vectors works.

I hope someone can help me with this :)

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
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>


using namespace std;

// type conversion prototype
double toNumber(const std::string & str);

int main() {

    ifstream theFile("~/Dropbox/Master/Cpp/Data/Edgedata.csv");

    // vectors used to store data from CSV file
    vector<string> FromNodes, ToNodes, AllNodes;
    vector<double> time;
    // Temporary variables to store elements in string stream
    double iTempTime;
    string tempFrom, tempTo, tempTime, line;


    while( getline(theFile, line)){ // read current line
        istringstream iss(line); // construct string stream from line

        // read line until comma saving value to temporary strings
        getline(iss, tempFrom, ',');
        getline(iss, tempTo, ',');
        getline(iss, tempTime, ',');

        // converting temporary edge weight string to temporary double value
        iTempTime = toNumber(tempTime);

        // csv file is sorted after nodes of origin but has many duplicates, I remove duplicates here, saving values to vectors for later use.
        if (tempTo != ToNodes[ToNodes.size()-1] || iTempTime != time[time.size()-1]) {
            FromNodes.push_back(tempFrom);
            ToNodes.push_back(tempTo);
            time.push_back(iTempTime);
        }
        // saving every new node in AllNodes vector to count number of nodes for use in Adjacency_list length
        if (tempFrom != AllNodes[AllNodes.size()-1]) {
            AllNodes.push_back(tempFrom);
        }
    }

    //Test to check if reading the CSV file and storing the data works properly
     cout << AllNodes.size() << endl;
     cout << FromNodes.size() << endl;


    return 0;
}

double toNumber(const string & str)
{
    istringstream iss(str);
    double num = 0;
    iss >> num;
    return num;
Before you use the stream you need to check if it is valid.

1
2
3
4
5
6
7
if (!theFile)
{
  cout << "File error" << endl;
  exit(1);
}
while( getline(theFile, line))


Did you check that the filename "~/Dropbox/Master/Cpp/Data/Edgedata.csv"is correct?
It seems the path to my file was incorrect, thank you for the hint Thomas! It seems C++ cannot use the ~ I had in my path :(
What happens when the vectors are empty, which element is accessed here:
AllNodes[AllNodes.size()-1]

I'd suggest something like
1
2
3
4
5
6
7
    if (AllNodes.empty())
       AllNodes.push_back(tempFrom);
    else 
    {
        if (tempFrom != AllNodes.back()) 
            AllNodes.push_back(tempFrom);
    }


http://www.cplusplus.com/reference/vector/vector/back/
http://www.cplusplus.com/reference/vector/vector/empty/
Last edited on
Thank you Chervil, that was precisely what my problem was, I kept on getting a segmentation 11 error since i wasn't aware of the .back feature in the vector class :)
Topic archived. No new replies allowed.