【发布时间】:2017-11-25 12:59:47
【问题描述】:
所以有一个代码,可以下载大小不大于 1024*100 字节的数据。代码来自https://rsdn.org/article/inet/inetapi.xml。
据我了解,InternetReadFile 在每次调用之后都应该继续读取字符数,否则它毫无意义,因为它会返回相同的数据。我红色,有一个函数,可以移动读取开始指针。我要使用它吗?
HINTERNET hInternetSession;
HINTERNET hURL;
char cBuffer[1024*100]; // I'm only going to access 1K of info.
BOOL bResult;
DWORD dwBytesRead;
// Make internet connection.
hInternetSession = InternetOpen(
L"tes", // agent
INTERNET_OPEN_TYPE_PRECONFIG, // access
NULL, NULL, 0); // defaults
// Make connection to desired page.
hURL = InternetOpenUrl(
hInternetSession, // session handle
L"https://www.google.com.ua/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png", // URL to access
NULL, 0, 0, 0); // defaults
// Read page into memory buffer.
while(bResult = InternetReadFile(
hURL, // handle to URL
(LPSTR)cBuffer, // pointer to buffer
(DWORD)1024 * 100, // size of buffer
&dwBytesRead)==TRUE&&dwBytesRead>0) // pointer to var to hold return value
// Close down connections.
InternetCloseHandle(hURL);
InternetCloseHandle(hInternetSession);
DWORD dwTemp;
HANDLE hFile = CreateFile(L"googlelogo_color_272x92dp.png", GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hFile) {
return 0;
}
WriteFile(hFile, cBuffer, sizeof(cBuffer), &dwTemp, NULL);
问题:我无法读取超过 1024*1024 字节,程序崩溃,当创建 char[1024*1024]
【问题讨论】:
-
char[1024*1024]- 这是函数中的局部变量吗? -
@RbMm,是的,都是本地的
-
所以你想要什么 - 默认堆栈大小为 1Mb - 这个数组吃掉所有堆栈,结果堆栈溢出
-
@RbMm,我想加载大于 100 kb 的数据
-
让 char cBuffer global 解决了这个问题 - 真的只说你在主题上的低技能。从哪里开始知道文件大小?如果您的硬编码大小缓冲区很大怎么办?