阵列太大?将大文件读入数组C++

Array too big? reading a big file into array C++

本文关键字:数组 C++ 阵列 文件      更新时间:2023-10-16

下面是我的代码,它不是很漂亮,但在许多情况下运行良好。
我遇到了这个问题,当我尝试将此文件(通过此程序下载)读取到数组中,然后对数组进行一些分析时,它会显示此错误:

项目II.exe 0x00a7c197处未处理的异常:0xC00000FD:堆栈溢出。

我觉得代码没有问题。是阵列的大小超过了内存容量吗?

或者您是否建议一种更好的方法来对文件进行分析,而无需将其读取到数组中?
谢谢!!任何帮助真的非常感谢!

#include <string>
#include <iostream>
#include <fstream>
#include <windows.h>
#include <wininet.h>
#include <winsock.h>
#include <stdio.h>
#pragma comment(lib, "wininet.lib")
using namespace std;
const int file_l      = 100;
const int file_length = 40000;
const int sam_url_num = 1;
wstring s2ws(const string& s)
{
    int len;
    int slength = (int)s.length() + 1;
    len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0); 
    wchar_t* buf = new wchar_t[len];
    MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
    wstring r(buf);
    delete[] buf;
    return r;
}
int download_url(string url)
{   
    HINTERNET hOpen, hURL;
    LPCWSTR NameProgram = L"Webreader";                                                         
    wstring stemp = s2ws(url);                                                                  
    LPCWSTR Website = stemp.c_str();
    char file[file_l+1];                                                                        
    unsigned long read;
    if ( !(hOpen = InternetOpen(NameProgram, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 )))
    {
        cerr << "Error in opening internet" << endl;
        return 0;
    }           
    hURL = InternetOpenUrl( hOpen, Website, NULL, 0, 0, 0 );                                    
    ofstream fout("RSS_archieve_download.txt");
    InternetReadFile(hURL, file, file_l, &read);
    int i = 0;
    while (read > 0)
    {                                                                                           
        file[read] = '';
        fout << file;
        InternetReadFile(hURL, file, file_l, &read);
        i+=1;
    }
    fout.close();
    cout << endl;
    InternetCloseHandle(hURL);
    return 0;
}
int read_file(string webpage[])
{ 
    ifstream fin("RSS_archieve_download.txt");
    if (fin.fail())
    {
        cout << "Failed to open RSS_archieve_download.txtn";
        exit(1);
    }
    int i = 0;
    while (true)
    { 
        getline(fin, webpage[i], 'n');
        if (fin.fail())
            break;
        i+=1;
    }
    fin.close();
}
void multi_url(string webpage[], string url_list[])//final printout func
{
    string url;
    ifstream fin("RSS_archieve_urls.txt");
    if(fin.fail())
    {
        cout << "Failed to open RSS_archieve_urlsn";
        exit(1);
    }
    int i = 0;
    while(true)
    {
        fin >> url_list[i];
        if (fin.fail())
        break;
        i+=1;
    }
    fin.close();
    ofstream fout("R2_url.txt");
    for(int j = 0; j < i; j++)
    {
        url = url_list[j];
        download_url(url);
//      read_file(webpage);
    }
    fout.close();
}
int main()
{
    string arr[file_length];
    string sample_url[sam_url_num];
    multi_url(arr, sample_url);
    system ("pause");
    return 0;
}

向量更适合处理大量数据的容器,并且更容易编码。

这是一个伪RSS提要阅读器。

#include <fstream>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) 
{
    vector<string> rssfeeds;
    string rssline;
    ifstream rssfile("rss.txt");
    int i=0;
    cout<<"RSS -----------------nn";
    while(getline(rssfile, rssline)) 
    {
        rssfeeds.push_back(rssline);

    }
    rssfile.close();
    for (i=0;i<rssfeeds.size();i++)
    {
        cout<<rssfeeds[i]<<"n";
    }
    return 0;
}

请参阅:从文件中读取多行字符串并将其存储在C++的字符串数组中