简体   繁体   English

将大型CSV文件读入数组C ++

[英]Read large csv file into array c++

I am trying to read a csv file with over 170000 rows with 10 columns each entry. 我正在尝试读取具有超过170000行,每项10列的csv文件。 I wrote this code using c++ (in visual studio 2017) to read it, but it only reads 3600 entries before failing. 我使用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;
}

Even if it could only read around 10000 entries, I'd call it a success 即使它只能读取10000个条目,我也称它为成功

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. 这就是为什么您的橡皮鸭想知道 3000的来源。 It's a guess, and anyone creating a 3001-line csv will likely crash your program. 这是一个猜测,创建3001行csv的任何人都可能会使您的程序崩溃。 If you think 10000 lines is a success, then 10001 lines is a crash. 如果您认为10000行是成功的,那么10001行就是崩溃。

Use std::vector . 使用std::vector It's an array-like structure. 这是一个类似数组的结构。 It manages its own size. 它管理自己的大小。 And it doesn't store your data on the limited stack. 而且它不会在有限的堆栈上存储您的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM