使用名称空间将C++转换为HTML

C++ to HTML conversion using namespaces

本文关键字:C++ 转换 HTML 空间      更新时间:2023-10-16

我正在尝试将.cpp文件转换为.html文件。

基本上,在程序结束时,当在chrome或其他任何东西上打开时,html文件应该看起来完全像:

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

我有三个文件,Source.cpp、fileToConvert.cpp和fileConverted.htm.

来源.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;
    }

然后输出应该看起来像上面的第一块代码,如HTML格式的所述。

我能做到这一点的唯一方法是将main()方法放在名称空间内的fileToConvert.cpp中,如下所示:

#include <iostream>
using namespace std;
namespace secondMain{
    int main()
    {
        int x = 4;
        if (x<3) x++;
        cout << x << endl;
        return 0;
    }
}

问题显然是,这将在.htm文件和中显示名称空间secondMain{…}代码,我不希望这样。

如果我不使用第二个名称空间,显然程序将无法工作,因为定义了两个main()方法。

这个程序缺少什么?我找到的唯一解决方法是添加第二个命名空间,并且我必须在该项目中使用命名空间,但不能在html页面中显示该命名空间定义。

任何信息都是值得赞赏的,谢谢

如果我理解正确,问题可能是您正在编译fileToConvert.cppSource.cpp