C++ ---> HTML

Pages: 12
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;

1
2
3
4
5
6
#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:

1
2
3
4
5
6
7
8
9
<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.
OK, I've written the code, I tested it and worked successfully. What should I do now?
so, what is the code for the conversion process.
how to convert it from C++ code to HTML code.
I need the documentation for doing the conversion process.
*cough cough* Homework assignment *cough cough*
I need the documentation for doing the conversion process.

And i need an entry level programmers job so I can get paid to do something I enjoy. But i'm only 18, and that's not going to happen. What's your excuse?
@Bazzy: I love you man. You're so full of win.

@bondzerg: Try writing a program that will copy a program character per character until it finds an EOF. Also, when it finds a < or a >, make it write something else. An if-else clause is recommended.

-Albatross

P.S.-We're not giving you the whole documentation.
@Albatross
After linking too many times to the "How to ask" I'm having a bit of fun
@Bazzy,
At first I was like :l but then I was like :D
Hi there, bondzerg! (do you play starcraft?) I m not allowed (and I don't want) to hand you the code for what you want to do... but it just happens that I've written something which does a similar job and I'd like to share it because it might be useful.

Do you have a facebook account? Well, there are many groups in facebook about c++ and in many discussions in these groups people often post code. However facebook trims spaces and tabs at the beginning of each line thus making the code hard to read.

The following program fixes this problem. Give it as input some code from fb posts and it will put tabs in the correct places. I want you to notice that not all '{' and '}' characters affect the tab placing. The ones that are located inside comments or chars or strings aren't taken into account. And I believe that's something you should also consider when writing code for your problem. (for example if a '<' character is inside a string you wouldn't want to replace it with "&lt")

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void print_tabs(ofstream & ofs,int tabs);
void pause();

int main()
{

	ofstream fout;
	ifstream fin;
	int tab_count;

	string file_in,file_out;

	cout << "enter input filename: ";
	getline(cin,file_in,'\n');
	cout << "enter output filename: ";
	getline(cin,file_out,'\n');

	fin.open(file_in.c_str());
	fout.open(file_out.c_str());

	if (!fin.is_open())
	{
		cout << "couldn't open input file...\nexiting now..."<< endl;
		pause();
		return 0;
	}

	tab_count=0;
	char prevc=0, prev2c=0, curc=0, nextc=0;
	bool in_char=false;
	bool in_string=false;
	bool in_new_comment=false;
	bool in_old_comment=false;
	bool in_comment=false;

	while (fin)
	{
		fin.get(curc);

		if (prevc=='\n')
		{
			in_new_comment=false;
			if (!in_old_comment) in_comment=false;
		}

		if (curc=='{' && !in_string && !in_comment && !in_char)
		{

			if (prevc!='\n')
			{
				fout << '\n';
				print_tabs(fout,tab_count);
			}

			fout << '{';
			tab_count++;
			nextc=fin.peek();
			if (nextc!='\n')
			{
				fout << '\n';
				print_tabs(fout,tab_count);
			}
		}
		else if (curc=='}' && !in_string && !in_comment && !in_char)
		{

			tab_count--;
			if (prevc!='\n')
			{
				fout << '\n';
				print_tabs(fout,tab_count);
			}

			fout << '}';

		}
		else if (curc=='\n')
		{

			fout << '\n';
			nextc=fin.peek();
			if (nextc=='}') print_tabs(fout,tab_count-1);
			else print_tabs(fout,tab_count);
		}
		else
		{
			fout << curc;
		}

		if (curc=='\'' && !in_comment && !in_string
		&& !(prevc=='\\' && prev2c!='\\') )
		{
			if (!in_char) in_char=true;
			else in_char=false;
		}

		if (curc=='"' && !in_comment && !in_char)
		{
			if (!in_string) in_string=true;
			else in_string=false;
		}

		if (curc=='/' && !in_string && !in_char)
		{
			if (prevc=='/' && !in_comment)
			{
				in_new_comment=true; in_comment=true;
			}
			else if (prevc=='*' && in_old_comment)
			{
				in_old_comment=false; in_comment=false;
			}
		}

		if (curc=='*' && !in_string && !in_char)
		{
			if (prevc=='/' && !in_comment)
			{
				in_old_comment=true; in_comment=true;
			}
		}

		prev2c=prevc;
		prevc=curc;
	}

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

	cout << "sourcecode fixed succesfully!\nexiting now..." << endl;
	pause();
	return 0;
}

void print_tabs(ofstream & ofs,int tabs)
{
	int i;
	for (i=0; i<tabs; i++) ofs << '\t';
	return;
}

void pause()
{
	cout << "hit enter to quit..." << endl;
	cin.get();
}

Last edited on
Why, exactly, did you feel the need to do this homework for him?
Last edited on
Try and tackle the problem in stages--isolate each one as much as possible when you have trouble. How about:
1. Start with a "Hello World" program that compiles.
2. Using fstream, open the input file and read it line by line using getline.
3. Process each line as you go, using string methods (see find and replace).
4. Output the result.

See:
http://cplusplus.com/reference/iostream/fstream/
http://cplusplus.com/reference/string/getline/
http://cplusplus.com/reference/string/string/
Why, exactly, did you feel the need to do this homework for him?

I don't think I m doing his homework... I m just giving him an example where he can see the basic logic of parsing c++ code. Note that what this example does is quite different from what his teacher asks... I use '{' and '}' to increase/decrease the tab counter and print tabs after each line. What he is asked to do is replace some '<' and '>'s with "&lt" and "&gt" respectively. If he is a total noob, he simply won't be able to just mod my example to fit his needs (maybe the size of the code will scare him too :D ), so it will be completely useless to him. On the other hand, if he knows the basics of the language he will find this example more helpful than the links to the site's tutorial. So, in any case, I believe I do more good than harm to him.
Well, I didn't actually read it, I just noticed that you posted some code and assumed that was why. I would still debate whether posting code is particularly useful, but whatever...
Thanks m4ster r0shi...
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <fstream>
#include <cassert>
#include <string.h>
using std::fstream;
using std::string;
using std::ios;

// extension file to be read
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;
}
Last edited on
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//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.
Last edited on
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...
Just ask the user to list the name of their file, get it with cin, and plug the string you got into fin.open and fout.open. Be sure to add the appropriate endings.

-Albatross
Last edited on
How I can do that...
Can you show me...
I already used string and declare it, but if I put the string inside the fin.open, there is an error...
Or maybe I have do something wrong it that coding...
Can anybody help me...
1
2
3
4
string fileName;
cout << "Enter the file name: ";
getline(cin, fileName);
fin.open(fileName.c_str());
@bondzerg,
The reason you got the error is because you didn't do this:
fin.open(fileName.c_str());

std::fstream::open() takes a C string parameter, you passed it a C++ string.
Pages: 12