0

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 << "&lt;";
        else if (next == '>')
            outStream << "&gt;";
        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!!

Greg Miller
  • 758
  • 6
  • 20

1 Answers1

2

If I understand correctly, the problem might be that you are compiling fileToConvert.cpp along with Source.cpp.

SimonFojtu
  • 413
  • 4
  • 12
  • Correct, but the file that has to be read must be a .cpp file; and this .cpp file to be read has to have all of the code that is going to be put into the html file, part of that code being read into the html file has a main method. So I'm not sure how to keep it from recognizing this second main method. – Greg Miller Nov 09 '15 at 17:07
  • The file to be converted certainly can have a `.cpp` extension, but you **must not compile it**. – SimonFojtu Nov 10 '15 at 09:52