c++编程 实现下载文件

 1 下载文件要用到操作系统的API函数,下面是一个WINDOWS系统中的实现:
2
3 //---------------------------------------------------------------------------
4
5 #include <stdio.h>
6 #include <windows.h>
7 #include <wininet.h>
8 #define MAXBLOCKSIZE 1024
9 #pragma comment( lib, "wininet.lib" ) ;
10
11 void download(const char *Url,const char *save_as)/*将Url指向的地址的文件下载到save_as指向的本地文件*/
12 {
13 byte Temp[MAXBLOCKSIZE];
14 ULONG Number = 1;
15
16 FILE *stream;
17 HINTERNET hSession = InternetOpen((LPCWSTR)"RookIE/1.0", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
18 if (hSession != NULL)
19 {
20 HINTERNET handle2 = InternetOpenUrl(hSession, (LPCWSTR)Url, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);
21 if (handle2 != NULL)
22 {
23
24
25 if( (stream = fopen( save_as, "wb" )) != NULL )
26 {
27 while (Number > 0)
28 {
29 InternetReadFile(handle2, Temp, MAXBLOCKSIZE - 1, &Number);
30
31 fwrite(Temp, sizeof (char), Number , stream);
32 }
33 fclose( stream );
34 }
35
36 InternetCloseHandle(handle2);
37 handle2 = NULL;
38 }
39 InternetCloseHandle(hSession);
40 hSession = NULL;
41 }
42 }
43
44 int main(int argc, char* argv[]){
45
46 download("http://www.baidu.com/","c:\\index.html");/*调用示例,下载百度的首页到c:\index.html文件*/
47 return 0;
48 }

以上,转自:http://zhidao.baidu.com/question/126310723.html


 

转载于:https://www.cnblogs.com/wang200/archive/2011/11/20/2256116.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值