问题
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;
auto pre_num_row = data_length.nc();
static const int num_row = 73;//data_length.nr();
static const int num_column = 74496; //data_length.nc();
std::cout << num_row << "\n";
std::cout << num_column << "\n";
double data[73][74496];
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] << "\n";
}
Hello, My name is Q. I want to read a csv file (here the name of the file is NAimg_20101026_145727.csv) and load the data to data array. The size of row and column of the file is 73 and 74496. Therefore, I need double data[73][74496] array. It makes error. When I make a small array, for example, double data[10][20], there is no error. Do you know what is the problem?
I have one more issue. I want to read the size of row and column from csv file and automatically decide the size of double data[row][column]. Therefore, I tried to use data_length.nc(); to decide the row and column of the double data size. Do you know how to decide the size of double data[row][column] from csv file? Thanks
回答1:
It seems that you'd like to create your 2D array based on data_length.nr() and data_length.nc() that may only be known at runtime so you want dynamic allocation. One simple way to do this is to #include <vector> and create it like this:
#include <vector>
// ...
// 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:
// Assuming csv lines formatted like: 1,2,3,4,5<newline>6,7,8,9,10<newline> ...
for(auto& row : data) {
for(auto& col : row) {
file >> col;
file.ignore(); // ignore one char (commas and newlines)
}
}
if(file) {
// successfully read the data from the file
std::cout << data[1][1] << '\n';
} else {
std::cout << "failed extracting data from file\n";
}
回答2:
The data array is placed on the stack, which is limited in size. My version allocates the array on the heap. But I guess you could add c++ operators.
#include <iostream>
#include <stdlib.h>
typedef struct DoubleArray2D{
int width = 0;
int height = 0;
double *data = NULL;
void allocate(int rows, int columns){
width = rows;
height = columns;
data = (double *)malloc(sizeof(double)*width*height);
}
void deallocate(){
width = 0;
height = 0;
free(data);
}
double get(int row,int column){
return data[column*height+row];
}
void set(int row, int column,double value){
data[column*height+row] = value;
}
}DoubleArray2D;
int main(){
DoubleArray2D data;
data.allocate(73,74496);
data.set(1,1,20.0f);
std::cout << data.get(1,1) << std::endl;
data.deallocate();
}
来源:https://stackoverflow.com/questions/65499929/2d-array-size-problem-loading-from-csv-file