14.7.1. HTMLfix.cpp: Character I/O Example

Review

The textbook, written in HTML, includes numerous C++ programming examples. However, C++ source code often contains three characters incompatible with HTML: <, >, and &. However, HTML defines appropriate character entities to represent incompatible characters. Manually replacing the characters, even using the "find-and-replace" feature provided by most text editors, is tedious and error-prone. HTMLfix is a simple program that replaces the incompatible C++ characters with their corresponding character entities. It also surrounds the program with opening and closing HTML pre tags.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    string input;                              // (a)
    cout << "Input file: ";
    getline(cin, input);
    ifstream in(input);

    if (!in.good())
    {
        cerr << "Unable to open " << input << endl;
        exit(1);
    }

    size_t ext = input.rfind('.');            // (b)
    string file = input.substr(0, ext);
    string output = file + ".html";
    ofstream out(output);

    if (!out.good())
    {
        cerr << "Unable to open " << output << endl;
        exit(1);
    }
    out << "<pre>";                            // (c)

    char c;
    while (in.get(c))                          // (d)
        switch(c)
        {
            case '<':                          // (e)
                out << "&lt;";
                break;

            case '>':
                out << "&gt;";
                break;

            case '&':
                out << "&amp;";
                break;

            default:                           // (f)
                out.put(c);
                break;
        }

    out << "</pre>" << endl;                   // (c)

    return 0;
}
 
HTMLfix.cpp. A program preparing C++ source code for publication on a webpage by replacing '<,' '>,' or '&' with HTML encodings. The program's first half prepares the files for reading and writing, while the latter half processes the files.
  1. Gets the program file name and opens and validates the file.
  2. Determines the output file name by replacing the ".cpp" extension of the program's name with ".html" and opens and validates it.
  3. Inserts some simple HTML code into the output file.
  4. The get function reads one character from the file, saving it in the variable c and looping while the error flags remain unset (i.e., until the stream reaches the end of the file).
  5. Detects and replaces incompatible characters with their HTML encodings.
  6. Copies compatible characters from the input to the output file unmodified.

Downloadable File

ViewDownloadComments
HTMLfix.cpp HTMLfix.cpp An example program reading and processing a file one character at a time.