Reputation: 1
I have a rather large two-dimensional vector of strings I'd like to write to a CSV file but my code crashes when I write. Any suggestions?
if (outfile.is_open()) {
for (int i = 0; i < width.size() - 1; i++) {
for (int j = 0; j < rows.size() - 1; j++) {
outfile << cells[i][j] << ",";
}
outfile << endl;
}
} else cout << "Unable to write.";
Declaration of width, rows and cell:
//Declare stuff
vector<string> rows, cols, width;
vector< vector<string> > cells;
string line;
//Open input file
std::ifstream infile("99-110.csv", ios_base::in);
std::ofstream outfile("99-110_corr.csv", ios_base::out);
//Create vector with each row as an index
while (getline(infile, line)) {
rows.push_back(line);
width = split(rows[0], ",");
}
//Split each vector index into a 2D vector
for (int i = 0; i < rows.size(); i++) {
cols = split(rows[i], ",");
cells.push_back(cols);
}
As it turns out I had rows.size() and width.size() mixed up. I swapped them and now everything seems to work for small files, but for files larger than around 500 MB my script crashes with terminate called after throwing an instance of 'std::bad_alloc'. It seems to be crashing during the for loop where I split each vector index into a 2D vector (last for loop provided above). The maximum size for the vector "cells" is 357,913,941 but I've calculated the actual size of the vector to be 97,780,085.
Upvotes: 0
Views: 1720
Reputation: 1582
Why do you need rows, cols, width
? cells
contains all the informations you need i suppose, so:
for (int i = 0; i < cells.size(); i++) { // start from i=1 to skip header
for (int j = 0; j < cells[i].size(); j++) {
you would read cells
with something like:
while (getline(infile, line))
cells.push_back(split(line, ","));
Upvotes: 1