0

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.

6
  • 3
    Any suggestions? use a debugger. Commented Jun 30, 2014 at 13:51
  • 1
    And cells really is an width.size() * rows.size() matrix? And the indexes are in the order in the code shown? Can you please show us the declaration and initialization of width, rows and cells? Commented Jun 30, 2014 at 13:53
  • @JoachimPileborg edited as requested. The split function referenced above is simply parsing a csv file and splitting each row by the commas. So yes, I'm sure cells is of columns width.size() and rows rows.size(). Commented Jun 30, 2014 at 14:02
  • uhmmm, wrong, you are changing width every time you put stuff in. you cannot be sure if it is the same size as cells[i].size(). Commented Jun 30, 2014 at 14:06
  • Right, but I know the amount of rows and columns will always be the same as the initial read. It's a square matrix. Commented Jun 30, 2014 at 14:08

1 Answer 1

1

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, ","));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.