HTML files use tags enclosed in angle brackets to denote formatting instructions. For example, <B> indicates bold and <I> indicates italic. If a web browser is displaying an HTML document that contain < or >, it may mistake these symbols for tags. This is a common problem with C++ files, which contain many <'s and >'s. For example, the line "#include <iostream>" may result in the browser interpreting <iostream> as a tag.
To avoid this problem, HTML uses special symbols to denote < and >. The < symbol is created with the string &lt; while the > symbol is created with the string &gt;.
Write a program that reads in a C++ source file and converts all < symbols to &lt; and all > symbols to &gt;. Also add the tag <PRE> to the begining of the file and </PRE> to the end of the file. This tag preserves whitespace and formatting in the HTML document. Your program should output the HTML file to disk.
As an example, given the following input file;

#include <iostream>
int main()
{
   int x = 4;
   if (x < 3) x++;
   cout<< x << endl;
}

the program should produce a text file with the following contents:

<PRE>
#include &lt;iostream&gt;
int main()
{
   int x = 4;
   if (x &lt; 3) x++;
   cout&lt;&lt; x &lt;&lt; endl;
}
</PRE>

you can test your output file by opening it with a web browser. The contents should appear identical to the original source code.

Recommended Answers

All 12 Replies

You need to do your own work. Start by creating a program that will copy 1 file to another a byte at a time.

Once you have that you can add filters to look for specific characters and output alternate character sequences as well as outputting extra text at the beginning and end of the file.

You will need to look up ifstream and ofstream in your documentation.

I don't know how to do the documentation and coding or the conversion process from C++ to HTML.
How to do it.
Really, I'm still blur of C++ programming.
The coding that I know in C++ is while, do while, for and simple code.

You original question question does not mention a need to do documentation. On the other hand if you mean you don't know how to look something up in your documentation how hard is it to pick up and open a book and check the index or to type something into Google.

As to the conversion from C++ to HTML the original text you posted detailed exactly what you have to do so I suggest you read what you have written.

Start by writing a program that reads the file one character at a time and write it to the screen. If you can do that, you're half-way there.

I just want to know how to do the coding and I have do some research..
In order to do that, I used the I/O File Stream and Data Files where the code is like this:

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

int main () {
  ofstream myfile ("example.txt");
  if (myfile.is_open())
  {
    myfile << "This is a line.\n";
    myfile << "This is another line.\n";
    myfile.close();
  }
  else cout << "Unable to open file";
  return 0;
}

This code will write a text "This is a line and This is another line to a example.txt only..
so, how can I write to the example.txt the whole program... start with #include <iostream> until return 0; }. In additional, it will start with HTML tag <PRE> and end with </PRE> and it will convert all the < and > symbols to &lt; &gt;.
I'm really new in the C++ language but still in learning process. So, I hope any master there can help me..
below is the sample C++ coding that need to be review...

#include <iostream>
#include <fstream>
#include <cassert>
#include <string.h>
using std::fstream;
using std::string;
using std::ios;

// file extension untuk dibaca
char *fileExt[] = {".c", ".cpp"};

void convert_to_html(string fileName);
string getProgName(char *szFileName);
void usage(const char *szProgName);


int main(int argc, char *argv[])
{
   std::cout << "\nHTML File Conversion program\n";
   if(argc < 2)
   {
      string sProgName = getProgName(argv[0]);
      usage(sProgName.c_str());
      return 1;
   }
   convert_to_html(argv[1]);
   std::cout << "File Conversion Succeeded!";
   return 0;
}


// Text File to Html File
void convert_to_html(string fileName)
{
   fstream fin(fileName.c_str(), ios::in);
   if(!fin.is_open())
   {
      std::cerr << "Error while opening \"" << fileName << "\"" << std::endl;
      assert(fin.good());
   }
   string fileName2 = fileName;
   int nExtNum = sizeof(fileExt)/sizeof(fileExt[0]);
   int nPos = -1, nLen = 0;
   for(int i = 0; i < nExtNum; ++i)
   {
      nPos = fileName2.find(fileExt[i]);
      nLen = strlen(fileExt[i]);
      if(nPos != string::npos && (nPos + nLen) == fileName2.length())
      {
         fileName2.erase(nPos, nLen);
         fileName2 += ".html";
         break;
      }
   }
   
   assert(fileName != fileName2);

   fstream fout(fileName2.c_str(), ios::out);
   if(!fin.is_open())
   {
      std::cerr << "Error while opening \"" << fileName2 << "\"" << std::endl;
      assert(fout.good());
   }
   fout << "<pre>\n";
   char cChar = 0;
   while(fin.get(cChar))
   {
      switch(cChar)
      {
      case '<':
         fout << "&lt;";
         break;
      case '>':
         fout << "&lt;";
         break;
      default:
         fout << "&#" << (int)cChar;
      }
   }
   fout << "</pre>\n";
   fin.close();
   fout.flush();
   fout.close();
}

string getProgName(char *szFileName)
{
   string sName = "";
   while(*szFileName++)
   {
      sName += *szFileName;
      if(*szFileName == '\\')
      {
         sName.erase();
      }
   }
   int nPos = sName.find(".exe");
   if(nPos == sName.length() - 5)
   {
      sName.erase(nPos, 5);
   }
   return sName;
}
   
void usage(const char *szProgName)
{
   std::cout << "Usage: " << szProgName << " <filename>" << std::endl;
}

So what you're saying is my suggestion was of no help. Sorry about that..

No.. Your suggestion is good..
But I just want to share some of the coding for the question..
That is the sample that I got from books and somewhere else...
I has review it but still got error in that coding...
I don't know how to correct the error..
So, I need help in that..

I has review it but still got error in that coding...
I don't know how to correct the error..

I've got an error is hardly a detailed description of actual program behaviour ans desired program behaviour (or a list of compiler error codes).

The more information you give the more likely it is that we will be able to help you.

I got the solution for my question..
After I have a discussion with my group members, lecturer and some notes from book, this is the coding for the program:

//This program will convert the selected file to another file for example .cpp to .html file.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

void addPlusPlus(ifstream& inStream, ofstream& outStream);

int main()
{
	ifstream fin;
	ofstream fout;

	cout<<"Begin editing files.\n";

	fin.open("convert.cpp"); //input file (must in the same folder)
	if (fin.fail())
	{
		cout<<"Input file opening failed.\n";
		exit(1);
	}
	
	fout.open("convert.html"); //output file (in the same folder)
	if (fout.fail())
	{
		cout<<"Output file opening failed.\n";
		exit(1);
	} 
        
        fout << "<PRE>"<< endl; //<PRE> is the tag for HTML file that will convert all the spacing according to the input file

	addPlusPlus(fin, fout);

	fout << "</PRE>" << endl; //</PRE> is the tag for HTML file that will close the <PRE> tag

	fin.close();
	fout.close();

	cout<<"End of editing files.\n";
	return 0;
}

void addPlusPlus(ifstream& inStream, ofstream& outStream)
{
	char next;

	inStream.get(next);

	while (!inStream.eof())
	{
	if (next == '<')
			outStream << "&lt;";
		else if (next == '>')
			outStream << "&gt;";
		else
			outStream << next;

		inStream.get(next);
	}
	
}

The output can be view in HTML format.
We can change the file that want to open/read but this file must in the same folder.
The output can be view either in HTML format, TXT format and others and it appears in that same folder.
So, thank again to everyone who helping me.

I have an additional question...
How can we choose what kind of .cpp file to be convert...
For example, in the same folder, there is exercise.cpp, try.cpp, convert.cpp...
So, can we choose the try.cpp to be convert to html...
How can I do that...
I have try several coding but still cannot...
So, can anyone help me...

From you last listing it is the file name hard coded on line 16 of your program.

If you mean how do you avoid hard coding the file name then either request the file name from the user at the start of the program or let the user specify the file name to use on the command line. You can access command line parameters by writing main as int main(int argc, char* argv[]) , any book or reference should let about using argc (the number of parameters) and argp (an array of pointers to the parameters).

Note that argv[0] is a pointer to the actual executable name.

I got the way to choose the input file name...

string fileName;
cout << "Enter the file name: ";
getline(cin, fileName);
fin.open(fileName.c_str());

I used that in my program and its works...
Anyway, thanks guy for helping me...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.