[C++]批量下载网络图片到本地

2016-11-18 写技术

最近看上了一个地图,在网上有电子版的,但是电子地图是由一小块一小块图片组成了,我想把他们下载到电脑上,图片太多,又不想一个地址一个地址地复制,怎么办?于是,写了个小程序:

#include <vector>
#include <urlmon.h>
#include <string>
#include <iostream>
using namespace std;
#pragma comment(lib, "urlmon.lib")
int main(int argc, char* argv[])
{
	int i, j;
	string url;
	string file;
	char tmp_url[1024];
	char tmp_file[100];
	wchar_t *buffer_url;
	wchar_t *buffer_file;
	size_t len;
	int nmlen;

	for (i = 0; i < 10; i++) {
		for (j = 0; j < 10; j++) {
			sprintf(tmp_url, "http://img.wikia.nocookie.net/intmap_tile_set_5360/4/%d/%d.png", i,j);
			sprintf(tmp_file, "%d_%d.png", i, j);
			url = tmp_url;
			file = tmp_file;

			len = url.length();
			nmlen = MultiByteToWideChar(CP_ACP, 0, url.c_str(), len + 1, NULL, 0);
			buffer_url = new wchar_t[nmlen];
			MultiByteToWideChar(CP_ACP, 0, url.c_str(), len + 1, buffer_url, nmlen);

			len = file.length();
			nmlen = MultiByteToWideChar(CP_ACP, 0, file.c_str(), len + 1, NULL, 0);
			buffer_file = new wchar_t[nmlen];
			MultiByteToWideChar(CP_ACP, 0, file.c_str(), len + 1, buffer_file, nmlen);

			URLDownloadToFile(NULL, buffer_url, buffer_file, 0, 0);
			delete[]buffer_url;
			delete[]buffer_file;
			cout << url << "  ->>>" << file << endl;
		}
	}


	system("pause");
	return 0;
}

标签: C++

发表评论:

Powered by anycle 湘ICP备15001973号-1