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
536 views
in Technique[技术] by (71.8m points)

c++ - Load 2D vector from CSV file (73x74496 data size)

I want to read csv file that the data is only numbers, the size of the data is (73x74496), and I want to load the data to vector sample which is in the code as follows. I would like to make sample[73][74496] vector. After that, I will do signal processing with sample[73][74496]. However, I cannot load the data of csv file to sample[73][74496] vector. Could you help me?

int main()
{
    //float data[38][27];
    double** data = new double[73][74496];
    std::ifstream file("NAimg_20101026_145727.csv");

    for (int row = 0; row < 73; ++row)
    {
        std::string line;
        std::getline(file, line);
        if (!file.good())
            break;

        std::stringstream iss(line);

        for (int col = 0; col < 74496; ++col)
        {
            std::string val;
            std::getline(iss, val, ',');
            if (!iss.good())
                break;

            std::stringstream convertor(val);
            convertor >> data[row][col];
        }
    }
    std::cout << data[1][1] << std::endl;
    return 0;
}

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

1 Answer

0 votes
by (71.8m points)

You can redefine how it std::ifstream interprets commas but saying that they too are just white space. Once that is done the rest falls easily into place.

#include <cassert>
#include <fstream>
#include <iostream>
#include <iterator>
#include <locale>
#include <vector>

namespace
{
struct CommaIsWhitespaceToo : std::ctype<char>
{
   static auto makeTable()
   {
      static std::vector<mask> v(classic_table(), classic_table() + table_size);
      v[','] |= space;
      return v.data();
   }
   CommaIsWhitespaceToo() : ctype(makeTable()) {}
};
}  // namespace

int main()
{
   int const kRows = 73;
   int const kCol  = 74496;
   std::vector<double> data;
   data.reserve(kRows * kCol);
   std::ifstream file("NAimg_20101026_145727.csv");
   file.imbue(std::locale(file.getloc(), new CommaIsWhitespaceToo));
   data.assign(
      std::istream_iterator<double>(file), std::istream_iterator<double>());
   assert(data.size() == kRows * kCol);  // Just make sure we got them all. Not
                                         // too many and not too few
   std::cout << data[(1 * kCol) + 1] << std::endl;
   return 0;
}

An alternative body where the data type uses [] access can be done like:

int main()
{
   constexpr int kRows = 73;
   constexpr int kCol  = 74496;

   using Row  = std::array<double, kCol>;
   using My2D = std::array<Row, kRows>;

   My2D data;
   std::ifstream file("NAimg_20101026_145727.csv");
   file.imbue(std::locale(file.getloc(), new CommaIsWhitespaceToo));
   for(auto& row : data)
      for(auto& col : row)
         file >> col;
   std::cout << data[1][1] << std::endl;
   return 0;
}

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

...