Thread: Downloading an image

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    2

    Downloading an image

    Hey guys, I am writing a program to download images.
    But the catch is that i need the file name to be read from a .mdb file.
    There are multiple filenames that I would need to pull from the database so that would need to be a variable.

    So basically I am asking how you can download an image from the web with the filename being a variable.

    Thank you ahead of time.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's the same as if it was hardcoded. As for downloading, I don't know, but I'm sure you'll find plenty of sample code at codeprojects.com.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2
    Thank you for your time

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I wrote some code to download images for a test application before, so I can post the main function here which does most of the work. Keep in mind this code is Windows specific, so it won't do you much good if you are using other platforms.

    Code:
    int Download(char* szFile,char* szLink)
    {
    	HINTERNET		hInternet,hURL;
    	char			contentLength[1000];
    	DWORD			fileSize;
    	unsigned long		clSize = 1000;
    	int			totalTransferred = 0;
    	unsigned char		*buff;
    	HANDLE			hFileWrite;
    	const int		readBuffSize = 5000;
    
    	hInternet = InternetOpen("app",INTERNET_OPEN_TYPE_DIRECT,0,0,0);
    	if (!hInternet)
    		return 0; 
    
    	hURL = InternetOpenUrl(hInternet,szLink,0,0,INTERNET_FLAG_NO_CACHE_WRITE,0);
    	if (!hURL)
    	{
    		InternetCloseHandle(hInternet);
    		return 0;
    	}
    
    	if (!HttpQueryInfo(hURL,HTTP_QUERY_CONTENT_LENGTH,contentLength,&clSize,0))
    	{
    		MessageBox(NULL,"Unable to get content length","Error",0);
    		InternetCloseHandle(hInternet);
    		return 0;
    	}
    	else
    		fileSize = atoi(contentLength);
    
    	buff = malloc(readBuffSize);
    
    	/* Open the file to write the contents */
    	hFileWrite = CreateFile(szFile,GENERIC_WRITE,0,0,OPEN_ALWAYS,0,0);
    
    	for (;;)
    	{
    		DWORD n;
    		DWORD dwBytesWritten = 0;
    
    		if(!InternetReadFile(hURL,buff,readBuffSize,&n))	
    		{
    			MessageBox(NULL,"There was an error attemtping to download file.","ERROR",0);
    			break;
    		}
    		if (n == 0)
    		{
    			/* EOF */		
    			break;
    		}
    		totalTransferred += n;
    		
    		WriteFile(hFileWrite,buff,n,&dwBytesWritten,0);
    	}
    	CloseHandle(hFileWrite);
    	free(buff);
    	InternetCloseHandle(hInternet);
    	return 1;
    }

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Note that this sample uses malloc/free, which is not recommended for C++. Instead use new/delete.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Note that this sample uses malloc/free, which is not recommended for C++. Instead use new/delete.
    It is fine to use malloc/free in C-code that is included inside C++ code - there's as far as I know, nothing to suggest that new/delete does anything significantly different from malloc/free, and malloc/free is certainly not "bad" in any way - they are of course not recommended for NEW C++ code. But if you are using existing C-code in a C++ project [like the above code], then that's a valid use.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading tiff image files?
    By compz in forum C++ Programming
    Replies: 9
    Last Post: 10-30-2009, 04:17 AM
  2. Replies: 1
    Last Post: 05-27-2009, 12:46 PM
  3. Simple Image Processing
    By ejohns85 in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2009, 12:10 PM
  4. HotSpot image controls (web)
    By novacain in forum C# Programming
    Replies: 0
    Last Post: 06-25-2008, 04:27 AM
  5. Replies: 4
    Last Post: 03-02-2003, 09:12 AM