C/C++轻松实现文件下载

47 篇文章 38 订阅

首先,我们先来了解几个API

_getcwd

Gets the current working directory.

char *_getcwd( 
   char *buffer,
   int maxlen 
);

buffer

Storage location for the path.

maxlen

Maximum length of the path in characters: char for _getcwd and wchar_t for _wgetcwd.

易知:buffer是路径

maxlen:是长度

功能是得到当前程序的路径


下一个API

URLDownloadToFile function

Downloads bits from the Internet and saves them to a file.

char *_getcwd( 
   char *buffer,
   int maxlen 
);
wchar_t *_wgetcwd( 
   wchar_t *buffer,
   int maxlen 
);



下面是代码:

#include <Windows.h>
#include <stdio.h>
#include  <direct.h>  
#pragma comment(lib,"URlmon")

int main()
{
	char buffer[MAX_PATH];
	_getcwd(buffer, MAX_PATH);
	strcat_s(buffer, "//1.jpg");
	HRESULT Result = URLDownloadToFileA(NULL, "http://112.22.245.11:443/down/de0243d60205717a2a74aea53c0c500c-46353/1.jpg?cts=yd-f-U13274580&ctp=111A23A228A125&ctt=1484747960&limit=1&spd=1300000&ctk=4ab73a4a76e47ad0b181d0f9fc47b6a1&chk=de0243d60205717a2a74aea53c0c500c-46353", buffer, 0, NULL);
	switch (Result)
	{
	case S_OK:printf("The download started successfully.\n");break;
	case E_OUTOFMEMORY: printf("The buffer length is invalid, or there is insufficient memory to complete the operation.\n"); break;
	}
	return 0;
}
程序运行截图:





评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT1995

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值