C++下载

前几天有个学妹问了个关于C++下载的问题。对C++我还真是很陌生,所以最近业余也试图学习点.

从网上扒了一下,不过网上的有点错误,有点小修改并加了个保存文件到磁盘,也当是自己学习下呃 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
#include < iostream >
#include
< fstream >
#include
< windows.h >
#include
< wininet.h >
#pragma comment(lib, "Wininet.lib")
using namespace std;
#define MAXBLOCKSIZE 1024

int main( void ()){
char isSave;
cout
<< " 是否保存文件到磁盘?(Y/N)\t " << endl;
cin
>> isSave;
char url[] = " http://www.baidu.com " ; // 网址或文件地址
char buffer[ 100000 ]; // 下载文件的缓冲区
DWORD bytes_read; // 下载的字节数
// 打开一个internet连接
HINTERNET internet = InternetOpen( " C++下载 " , INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, NULL);
// 打开一个http url地址
HINTERNET file_handle = InternetOpenUrl(internet, url, NULL, 0 , INTERNET_FLAG_RELOAD, 0 );
// 从url地址中读取文件内容到缓冲区buffer
BOOL b = InternetReadFile(file_handle, buffer, 100000 , & bytes_read);
buffer[bytes_read]
= 0 ;
if (isSave == ' Y ' ){
fstream fs;
// 写入文件这里可根据自己需要写,
fs.open( " D:\\1.html " ,fstream:: in | fstream:: out | fstream::app);
fs.seekg(
0 ,ios::end);
fs
<< buffer;
fs.close();
}
cout
<< buffer << endl;

InternetCloseHandle(internet);

char pause;
cout
<< " \n\t\r==============下载完了======= " << endl;
cin
>> pause;
}

转载于:https://www.cnblogs.com/OOGG/archive/2010/11/20/1882629.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值