Programming an HTML Downloader in C++
Written By: Alwyn Malachi Berkeley
- 25 Sep 2006 -
Description: This program requests the source code (HTML) of a webpage and then gives the user multiple options for viewing the web server's response. We will use the Winsock API to manipulate sockets on the system, and create reusable code along the way.
- Creating the Project
- main.cpp Headers and the Internet Namespace
- The Webpage Class
- The Internet Namespace's Functions
- GetDomain
- Custom_itoa
- GetWebpage
- SaveFile
- Internet.hpp and Writing main.cpp
- Compile and Conclusion
Implementing Custom_itoa
The Custom_itoa function is very simple. Most compilers define a variable called itoa(integer-to-alphanumeric) but they don't have to because that function is not part of the ANSI standard. Many compilers just choose to define the itoa function because it is beneficial to programmers. So in order make our code more portable between compilers we are going to write a function that does the same function. This way we can be sure that no matter what compiler that's used, the source code will definitely compile without complications. Add the source code for Custom_itoa to the "InternetHelperFunctions.cpp" file. The Custom_itoa source code is below:
const char* Custom_itoa(int intParam) { using std::stringstream; // convert from integer to string stringstream theTempStream; theTempStream << intParam; return (theTempStream.str()).c_str(); }
What we do is declare an object from the std namespace called stringstream. The object does pretty much what it sounds like; it streams strings. What we do is give the stringstream an integer value via the insertion operator. Then we use the str() method in stringstream to return a string object. Once we have the string object we use the c_str() method of the string object to return a C-style string(character array).
The code is very simple and portable. That is all there is to this function. Now we can continue on to the most important function in the project.