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

时间:2013-10-29 02:50:59

标签: c++ arrays visual-studio-2010

下面是我的代码,它不是很漂亮,但在很多情况下效果很好 我正在遇到这个问题,当我试图将这个文件(通过这个程序下载)读入数组时,然后对数组做一些分析,它显示了这个错误:

projectII.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] = '\0';
        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.txt\n";
        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_urls\n";
        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;
}

1 个答案:

答案 0 :(得分: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 -----------------\n\n";

    while(getline(rssfile, rssline)) 
    {

        rssfeeds.push_back(rssline);


    }
    rssfile.close();

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

    return 0;
}

请参阅:Reading multiple lines of strings from a file and storing it in string array in C++