0

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
        }

1 Answers1

0

You may want to check your code for common errors. Something like this will never compile:

int c;
double c;

You also do not have a main() function

Because of these errors this may not fit your needs exactly. Assuming each line contains three ints, four doubles, an int, and then two doubles, I would declare a "linedata" class like so:

#include <iostream>  // for std::string
#include <string>    // for std::string
#include <sstream>   // for std::istringstream
#include <utility>   // for std::forward
#include <fstream>   // for std::ifstream
#include <vector>    // for std::vector
#include <stdexcept> // for std::runtime_error

class linedata
{
    int a;
    int b;
    int c;
    double d;
    double e;
    double f;
    double g;
    int h;
    double i;
    double j;

public:

    // constructs a linedata object
    linedata(std::string&& line)
    {
        std::istringstream ss( std::forward<std::string>(line) );
        char comma;
        ss >> a >> std::ws >> comma >> std::ws >> // std::ws discarding extra spaces
              b >> std::ws >> comma >> std::ws >>
              c >> std::ws >> comma >> std::ws >>
              d >> std::ws >> comma >> std::ws >>
              e >> std::ws >> comma >> std::ws >>
              f >> std::ws >> comma >> std::ws >>
              g >> std::ws >> comma >> std::ws >>
              h >> std::ws >> comma >> std::ws >>
              i >> std::ws >> comma >> std::ws >>
              j;

        // check for errors. throw if it fails to parse
        if( ss.fail() ) throw std::runtime_error("Could not parse line!");

        std::cout << a << '\n' << b << '\n' << c << '\n' << d << '\n' <<
                     e << '\n' << f << '\n' << g << '\n' << h << '\n' <<
                     i << '\n' << j << std::endl;
    }
};

Then, in main() you can try opening the file to parse the strings:

int main()
{
    std::vector<linedata> lines;

    std::ifstream file("mynumbers - Copy.csv");
    if( ! file.is_open() )
        // personally I would not use perror because it is part of c and 
        // requires yet another header. I'd use this:
        std::cerr << "Error occurred: " << std::strerror(errno) << std::endl;

    std::string myline;
    std::getline(file,myline); // the '\n' is not necessary. It gets a line.
                               // it already knows to look for '\n'.

    while( std::getline(file,myline) )
    {
        lines.emplace_back(std::move(myline));
    }
}
  • 1
    Your `comma` input could easily eat whitespace instead of a comma. It would be an extra safeguard to use `ss >> a >> std::ws >> comma >> b >> std::ws >> comma >> ...`. You might want to set an error state or throw in case the `linedata` construction did not successfully parse the string. – paddy Mar 27 '17 at 08:16
  • there is no need to use the std::: when you have using namespace std this makes code look ugly – looneytunes24 Apr 14 '17 at 06:04
  • @looneytunes24 I would recommend never `using namespace std;` as would many programmers. What I am actually doing is something that is considered a good practice. See http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Dustin Nieffenegger Apr 14 '17 at 07:38