A general question

This is a discussion on A general question within the C Programming forums, part of the General Programming Boards category; Hi there and hello to all, I was just wondering if it is possible to access an online file posted ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    4

    Question A general question

    Hi there and hello to all,

    I was just wondering if it is possible to access an online file posted on a website thru C codes. I mean if we are using, lets say, Microsoft Visual C++, is there any commands to read a line from a txt file posted on http://www.cprogramming.com/test.txt (for example) ???

    I'd appreciate to hear from you or any extra info that you might have.

    Tanx.

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    there are platform specific ways to do it, with windows you would use winsock or one of the wrapper classes, for instance wininet api, look into using that.

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Code:
    #include <windows.h>
    #include <wininet.h>
    
    #pragma comment(lib, "winInet.lib")
    
    #define MAX_INTERNETFILE_LENGTH 20480
    
    char* DownloadFile(char* strURL,HWND hwnd)
    {
    	BYTE data[MAX_INTERNETFILE_LENGTH];
    
    	HINTERNET hInternet=NULL;
    	hInternet=InternetOpen("agent name",INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
    	HINTERNET hOpenURL=InternetOpenUrl(hInternet,strURL,NULL,0,INTERNET_FLAG_DONT_CACHE,0);
    
    	DWORD dwSize=0;
    	DWORD dwOffset=0;
    	DWORD dwNumBytesRead=0;
     	BOOL returnValue=TRUE;
    
    	char* vData=(char*)malloc(1);
    	while(returnValue)
    	{
    		returnValue=InternetReadFile(hOpenURL,(VOID*)data,MAX_INTERNETFILE_LENGTH,&dwNumBytesRead);
    		if(!dwNumBytesRead)
    		{
    			if(!returnValue)
    				MessageBox(hwnd,"Error downloading file.","Error",MB_OK);
    			break;
    		}
    		dwSize+=dwNumBytesRead;
    		vData=(char*)realloc((void*)vData,dwSize+1);
    		memcpy(vData+dwOffset,(void*)data,dwNumBytesRead);
    		dwOffset+=dwNumBytesRead;
    	}
    
    	*(vData+dwOffset)='\0';
    
    	InternetCloseHandle(hOpenURL);
    	InternetCloseHandle(hInternet);
    
    	return vData;
    }
    Won't return until all data has been downloaded for that file. It returns (as a char*) the data for the file, which you must delete when done with.

    Windows-specific, mind you.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    Thanks for the code.

    Although I couldn't use the function and got so many errors that couldn't fix.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Are you using a Windows compiler?

    Code:
    char* vData=(char*)malloc(1);
    You shouldn't cast malloc(). And you should free() the memory when you're done with it.

    I couldn't use the function and got so many errors that couldn't fix.
    With that function, or the rest of the code?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    Yes, I am using a windows compiler. (MS Visual)

    When i try to compile this code, i get 23 errors. the only one that I can fix is in this line:

    Code:
     HINTERNET hOpenURL=InternetOpenUrl(hInternet,strURL,NULL,0,I  NTERNET_FLAG_DONT_CACHE,0);
    As you see there is a space between 'I' and 'NTERNET' so:


    Code:
     HINTERNET hOpenURL=InternetOpenUrl(hInternet,strURL,NULL,0,INTERNET_FLAG_DONT_CACHE,0);
    which reduces the errors to 19. but about the rest, I have no idea. and unsure when to free the malloc. (right at the end, before 'return' wouldn't cause a problem would it?)

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Do you #include <windows.h>?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    Yes, I do include <windows.h> . The first line of the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A general question aout programming?
    By bradt93 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-26-2008, 11:00 AM
  2. winsock - general question
    By keira in forum Windows Programming
    Replies: 1
    Last Post: 09-28-2007, 01:56 PM
  3. another exercise question
    By luigi40 in forum C# Programming
    Replies: 3
    Last Post: 11-28-2005, 02:52 PM
  4. general programming compatability question
    By Metarectilinear in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-25-2002, 11:51 PM
  5. general question regarding a function parameter
    By mlupo in forum C Programming
    Replies: 7
    Last Post: 10-13-2002, 07:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21