阵列太大?将大文件读入数组C++
Array too big? reading a big file into array C++
下面是我的代码,它不是很漂亮,但在许多情况下运行良好。
我遇到了这个问题,当我尝试将此文件(通过此程序下载)读取到数组中,然后对数组进行一些分析时,它会显示此错误:
项目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++的字符串数组中
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '