dimanche 26 mars 2017

Parsing data of string in vector to appropriate data type c++

hello all I'm reading data from a CSV file and have successfully parsed all the characters out of the data now i need to transform the string data in each column into its appropriate type (int,double,etc..). I have the following types declared that i want and then from there each variable should be updated for as many lines that come in so i can take that variable and call it somewhere else in the program. Please see Below

vector<std::vector<std::string> > matrix;
int a;
int b;
int c;
double c;
double d;
double e;
double f;
int f;
double f;
double f;
string line;
ifstream file("mynumbers - Copy.csv");

if (!file.is_open())
    perror("error while opening file");

    getline(file, line, '\n');//get the first line of columns names out

while (getline(file, line,'\n')) //find the endline charcater
    {

        vector<string> vec;
        string linevec;
        istringstream ss(line);//allows for parsing each line 

        while (getline(ss, linevec, ','))
        {

            vec.push_back(linevec);//push the parsed data for that line into a vector
        }

matrix.emplace_back(vec);//push each parsed line into another vector
        }

Aucun commentaire:

Enregistrer un commentaire