Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

c++ - making 2D array from csv file

I made this code based on the suggestion from Ted. I want to read data from csv file and load the data on data array. The code the above does not work. Do you know the problem?

int main()
{

    auto base_dir = fs::current_path();
    auto dataset_1 = data_names_1;
    auto dataset_name = base_dir / "NAimg_20101026_145727.csv";

    //double data[10][3];
    std::ifstream file(dataset_name);

    matrix<DataType> data_length;
    //file >> data_length;

    // create a 2D vector from nr() and nc()
    std::vector<std::vector<double>> data(data_length.nr(),
        std::vector<double>(data_length.nc()));

    // fill the 2D vector with data from the file using range-based for loops:
    for (auto& row : data) {
        for (auto& col : row) {
            file >> col;
        }
    }

    if (file) {
        // successfully read the data from the file
        std::cout << data[1][1] << '
';

    }
    else {
        std::cout << "failed extracting data from file
";
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...