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 SaveFile
This is a simple straight forward function and it also goes in the "InternetHelperFunctions.cpp" file. The SaveFile() simply saves the data that is passed in through it's parameters. The source code is as followed:
const bool SaveFile(const string &strFileNameParam, const string &strSaveDataParam) { // saves a file using std::ofstream; // open the file ofstream OutwardStream; OutwardStream.open(strFileNameParam.c_str(), std::ios::out); // insert the data OutwardStream << strSaveDataParam; // close the file OutwardStream.close(); }
As you can see this code is very simple. It opens a file handle for output. Then it sends data into the file that was opened. Lastly it closes the file handle. This is an open and shut case, no further details needed.