How to read CSV file and assign to Eigen Matrix?

后端 未结 3 1305
再見小時候
再見小時候 2020-12-19 15:27

I try to read a large cvs file into Eigen Matrix, below the code found having problem where it can not detect each line of \\n in cvs file to create multiple rows in the mat

3条回答
  •  太阳男子
    2020-12-19 16:03

    This will read from a csv file correctly:

    std::ifstream indata;
    
    indata.open(filename);
    
    std::string                line;
    while (getline(indata, line))
    {
        std::stringstream          lineStream(line);
        std::string                cell;
    
        while (std::getline(lineStream, cell, ','))
        {
            //Process cell
        }
    }
    

    Edit: Also, since your csv is full of numbers, make sure to use std::stod or the equivalent conversion once you expect to treat them as such.

提交回复
热议问题