Top 3 Products & Services
Dated: Nov. 08, 2004
Related CategoriesC Programming / C++ / C#
You may copy/paste this follow C++ Code and save it as "WebReader.cpp".
|//Read a web page
//By: Ricky Shadrach
//This lets us output to the console
//This is needed for the inclusion of wininet.h
//This is where all of our awsome functions that we call are
#pragma comment (lib, "wininet.lib")
//If you have never seen this line above before, it is a great line of code to know.
//We have all our function proto-types provided in the header file wininet.h, but if you
//look in wininet.h, there is no actual code which makes these functions work. All of
//this code is actually in wininet.lib. Without this line of code, you would either need to
//add extra syntax to include this library or you would need to change your compiler
//settings. Either way, this line makes it much easier as it includes the file for you.
HINTERNET hOpen, hURL;
//these are two handles which we will use to access the internet
hOpen = InternetOpen("WebReader", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
//This first call is fairly simple. It just returns a handle to the url. The first
//parameter is just the name of our program which calls this function. The second is
//how we are accessing the internet. The last three are not important to us, if you would
//like to learn about these, go to the link at the bottom of this file.
hURL = InternetOpenUrl( hOpen, "http://www.google.com", NULL, 0, 0, 0 );
//Once again, we get a handle from this function. However, this time, now we have access to
//read from the file, which is what this function allows you to do. The first paramenter
//is the handle that we recieved from InternetOpen(). It contains the name of our program
//as well as the access type which will be needed for this function to open a URL for reading
//The next parameter should be obvious,it is the URL we wish to open. The last 4 are not
//important to us, if you wish to learn more, go to the link at the bottom of this file.
//this is where our file will be stored temporarily. You can make this as big as you
//want. The larger it is, the faster you will read and display the file. If you can get
//it all in one function call, that is the fastest it can go. However, the difference is
//not great. In the file we are about to read in, it takes an average of 13.51 milliseconds
//longer to read the file one character at a time than it does to read it all in one shot.
unsigned long read;
//this will let us know how many characters actually got read. When it doesn't return
//the total requested, we know we have reached the end of the file.
InternetReadFile(hURL, file, 100, &read);
//Here is the function which reads our URL. The first paramenter is the handle returned
//from InternetOpenUrl(). The second is the where the characters from the file will go.
//The third is how many character we request to read. This should normally be the size
//of the array you pass in (file in this case). The fourth is how many characters
//actually got returned. We use this to tell if we are at the end of the file.
file[read] = '\0';
//Set the last character that was read in to '\0' to terminate the string. This prevents
//from unwanted output such as random weird characters because the characters are not
cout << file;
//Display the file.
} while (read == 100);
//Here we check if we are at the end of the file. If the number of characters read equals
//the number of characters requested, we know that there is still more in the file (unless
//we hit the end of the file perfectly at 100, in which case the next call to
//InternetReadFile() will return 0 anyways). If we keep on calling InternetReadFile() after
//the end of the file has passed, it will start over from the top.
cout << endl;
//just to make the output look nice.
//wininet is actually quite a powerful api, it can do many more things than just read a file.
//Check it out:
//Possibilites with this code for future projects:
//Check all the links on a page to see if they are valid
//Webcrawler which crawls across the entire internet
//Save web pages to html files on your computer
//Craw across a domain using the links from the main page, saving the files as you go.
//Send any questions or comments to email@example.com
Now that you've gotten free know-how on this topic, try to grow your skills even faster with online video training. Then finally, put these skills to the test and make a name for yourself by offering these skills to others by becoming a freelancer. There are literally 2000+ new projects that are posted every single freakin' day, no lie!