I'm trying to convert a .cpp file into a .html file.
Basically, at the end of the program, the html file when opened on chrome or whatever should look exactly like:
#include <iostream>
using namespace std;
int main()
{
int x = 4;
if (x < 3) x++;
cout << x << endl;
return 0;
}
I have three files, Source.cpp, fileToConvert.cpp, fileConverted.htm.
Source.cpp:
//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 conversion(ifstream& inStream, ofstream& outStream);
int main()
{
ifstream fin;
ofstream fout;
cout << "Begin editing files.\n";
fin.open("fileToConvert.cpp"); //input file (must in the same folder)
if (fin.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
fout.open("fileConverted.htm"); //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 conversion(ifstream& inStream, ofstream& outStream)
{
char next;
inStream.get(next);
while (!inStream.eof())
{
if (next == '<')
outStream << "<";
else if (next == '>')
outStream << ">";
else
outStream << next;
inStream.get(next);
}
}
fileToConvert.cpp:
#include <iostream>
using namespace std;
int main()
{
int x = 4;
if (x < 3) x++;
cout << x << endl;
return 0;
}
And then the output should look like the first block of code above as said in HTML format.
The only way I can get this to work is to place the main() method in fileToConvert.cpp inside of a namespace, like so:
#include <iostream>
using namespace std;
namespace secondMain{
int main()
{
int x = 4;
if (x<3) x++;
cout << x << endl;
return 0;
}
}
Problem obviously being, this will display the namespace secondMain{...} code inside of the .htm file and, which I do not want.
If I don't use this second namespace, obviously the program will not work since there are two main() methods defined.
What am I missing in this program? The only workaround I found was adding that second namespace, and I do have to use namespaces in this project, but cannot display that namespace definition in the html page.
Any information is appreciated, Thanks!!