0

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

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;
}
4

1 回答 1

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;
}

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

于 2013-10-29T16:06:45.797 回答