1

I am trying to read a csv file with over 170000 rows with 10 columns each entry. I wrote this code using c++ (in visual studio 2017) to read it, but it only reads 3600 entries before failing.

// Trial1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <sstream>

using namespace std;
int main()
{

    ifstream file("Brightest Day.csv");

    if (!file.is_open())
    {
        cout << "ERROR: File Open" << "\n";
    }

    string data[3000][10];

    for (long i = 0; i < 3000; i++)
    {

            for (int j = 0; j < 10; j++)
            {
                getline(file, data[i][j], ',');

            }

    }

    for (long i = 0; i < 3000; i++)
    {

        for (int j = 0; j < 10; j++)
        {
            cout<<data[i][j]<<" | ";
            if (j == 10)
            {
                cout << "\n";
            }
        }
    }
    return 0;
}

Even if it could only read around 10000 entries, I'd call it a success

8
  • 7
    Rubber ducky wants to know what the significance of 3000 is. Commented May 14, 2018 at 19:43
  • 2
    if (!file.is_open()) { cout << "ERROR: File Open" << "\n"; } looks like botched error handling to me. If the file is not open you print an error but continue anyway. Commented May 14, 2018 at 19:45
  • 6
    In what way does this program fail? It's not enough to simply say "it reads 3600 before failing". Are you getting an error message? Is the program crashing? Is it failing to compile? What kind of failure is occurring? Commented May 14, 2018 at 19:46
  • 3
    Uhhh... for (long i = 0; i < 3000; i++)... over 170000 rows .... i < 3000... Commented May 14, 2018 at 19:49
  • 4
    With over 170000 rows you are most likely going to run out of stack space if you keep trying to store your strings there (string data[3000][10]; - besides, that array is never going to hold 170000 strings, no matter how hard you really want it to) - use a std::vector. Commented May 14, 2018 at 19:58

1 Answer 1

5

You are overflowing your stack. Welcome to this website.

Your call stack is designed for small objects whose sizes are known at compile time. That's why your rubber ducky is wondering where 3000 came from. It's a guess, and anyone creating a 3001-line csv will likely crash your program. If you think 10000 lines is a success, then 10001 lines is a crash.

Use std::vector. It's an array-like structure. It manages its own size. And it doesn't store your data on the limited stack.

Sign up to request clarification or add additional context in comments.

1 Comment

Addendum: The math goes down something like this: in MSVC 32 bit, a std::string is 28 bytes 28 *10 = 280 bytes per row. 3600 rows is 1008000 bytes. The standard Windows stack is 1MB, so very little wiggle room is left for anything else. If compiling for 64 bit, pointers are bigger so std::string is bigger. You can probably only get about 2600 rows in 64 bit.

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.