将大型csv文件读入数组c ++

rum*_*ghs 1 c++ csv

我试图读取一个超过170000行的csv文件,每个条目有10列.我使用c ++(在visual studio 2017中)编写了这段代码来阅读它,但它在失败之前只能读取3600个条目.

// 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;
}
Run Code Online (Sandbox Code Playgroud)

即使它只能阅读大约10000个条目,我也称之为成功

Dre*_*ann 5

你堆满了.欢迎来到这个网站.

您的调用堆栈是为在编译时已知大小的小对象而设计的.这就是为什么你的橡皮鸭想知道 3000来自哪里.这是一个猜测,任何创建3001行csv的人都可能会崩溃你的程序.如果您认为10000行是成功的,那么10001行就是崩溃.

使用std::vector.这是一个类似阵列的结构.它管理自己的大小.并且它不会将数据存储在有限的堆栈中.