cocos2dx之curl下载

本文介绍了一个使用C++实现的网络文件下载功能。该功能利用libcurl库进行HTTP请求,并将远程服务器上的文件下载到本地指定路径。文章详细展示了如何初始化CURL句柄、设置下载URL、创建目标文件及执行下载过程。
int NQDownload::NQDown_src( const char* s_outfile,const char* plist_url )
{
	CURL *curl;
	CURLcode res;
	char buffer[10];
	curl = curl_easy_init();
	string _list=s_outfile;
	string _path_invers,_path="md ";
	int _count=0;
	for (int _index=_list.size();_index>0;_index--)
	{
		if (_list[_index-1]=='/')
		{
			_count++;
			if (_count==1)
			{
				_count=_index-1;
				break;
			}
		}
	};
	_path_invers=_list.substr(0,_count);
	for(int _pindex=0;_pindex<_path_invers.size();_pindex++)
	{
		if (_path_invers[_pindex]=='/')
		{
			_path.push_back('\\');
		}
		else
			_path.push_back(_path_invers[_pindex]);
	};
	system(_path.c_str());
	if (curl)
	{  
		//创建写入的文件。  
		FILE *outfile;
		outfile = fopen(s_outfile,"wb");

		curl_easy_setopt(curl, CURLOPT_URL, plist_url);
		if(outfile)
		{
			//指定写入的文件指针。  
			curl_easy_setopt(curl, CURLOPT_FILE, outfile);
		}
		//设置写数据的回调函数。  
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
		res = curl_easy_perform(curl);
		curl_easy_cleanup(curl);
		//关闭文件  
		fclose(outfile);
		return 1;
	}
	else
	{
		return -1;
	}
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值