Post-lab - Lab (5)



The purpose of this assignment is to create a program that works like a simple html file converter.  Your program will read a text file and will produce a html file that can be viewed using a web browser, like Netscape or Internet Explorer.

Write a C++ code that converts an input file, myfile, into a simple Hyper Text Markup Language (HTML) file, myfile.html. In order to do this, you will read the input file, myfile, from the beginning to the end and will create the output file, myfile.html, by following the instructions given below.

1) Every time you run your code, the first thing your program will do is to write the following 5 lines at the beginning of the output file before you start reading the input file.  This is a very minimum header for your html file.  You will use a function called write_header to do this part of the program.

<Html>
<Title>
This is my C++ html converter
</Title>
<Body>

2) From this point on, you will read the contents of the input file, myfile, and will copy it to the output file, myfile.html. However, as the input file is read, you will write a "<br>" (without the ") into the output file each time a newline is reached.  The <br> in html means the same thing as the "\n" in C++.  Continue reading and copying the rest of the file.  Repeat this as needed until you are done reading the entire file.

For example, suppose your input file has the following two lines:
Line 1 on the input file: I have a black cat.
Line 2 on the input file: I have a white dog.

As you have noticed, at the end of the first line there is an invisible "\n". So, once you reached that, your program will write <br> into the output file. Thus, these two lines in the output file will look like this:

I have a black cat. <br>
I have a white dog.  <br>

3) After reading the input file and copying its entire contents into the output file, you will write the following two lines at the end of the output file. You will use a function called write_footer to write these lines.

</Body>
</Html>

The output that you will generate after you run the program will be an html file.  If you have done everything correctly, you can open it using a web browser like Netscape or Internet Explorer.

A sample input file:

Hi my name is David.
I wrote this program that uses the file I/O to create html files from text files.

Sample output:
<Html>
<Title>
This is my C++ html converter, YourName.
</Title>
<Body>

Hi my name is David. <br>
I wrote this program that uses the file I/O to create html files from text files.  <br>

</Body>
</Html>

Your program should include the following functions:
void instructions( )
//Displays instructions on how to run the program and what the input is.

void get_test_files(ifstream& in_s, ofstream& out_s)
// This function asks users to enter the input and output file names from the keayboard.  It will attempt to connect to the files and once successfully connected, it returns the input and output streams to the main.  If it fails to connect to the input or output file, it will terminate the program.

void write_header(ofstream& out_s)
// This function will write the 5 lines of header into the output file.

void write_footer(ofstream& out_s)
// This function will write the last 2 lines, the footer of the html file.

void copy_text(ifstream& in_s, ofstream& out_s)
// This file copies the entire contents of the input file, myfile, into the output file, myfile.html, line-by- line, by considering the given instruction for the newline.