C++ How to format a text file into an html file

I am very new to C++. I have the building blocks for what I need my program to do, which is: Read a text file inputted by command line (ex. ./textToHtml.exe Alien.txt) and create an html file from the text file by implementing the correct HTML tags when necessary. I have provided the code below, as well as the html file structure, this is for a project and I need to have the line breaks
after each paragraph and each line blank line, I have provided the last html structure as what I want it to look like.

Be advised, I am sure I have some unnecessary lines or redundant code.

 #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
   int main(int argc, char *argv[])
{
    std::ifstream txtFile(argv[1]);

    std::string fn = argv[1];
    std::string fileName = fn.substr(0, fn.size() - 4);
    if (txtFile)
    {
        std::ofstream html(fileName + "1.txt");
        if (html)
        {
            html << "<HTML>\n"
                 << "<head>\n"
                 << "<title>";
            std::string line{};
            if (std::getline(txtFile, line))
            {
                html << line << "</title>" << '\n';
            }
            html << "</head>\n"
                 << "<body>\n";
            while (std::getline(txtFile, line))
            {
                html << line << "<br>" << '\n';
            }
            html << "</body>" << '\n'
                 << "</html>" << '\n';
        }
    }
    return 0;
}

The HTML file looks like this:

<HTML>
<head>
<title>Are These Aliens Martians?</title>
</head>
<body>
<br>
<br>
an adaptation<br>
<br>
The men from Earth stared at the aliens.<br>
The little green men had pointed heads and<br>
orange toes with one long curly hair on each toe.<br>
<br>
H. G. Wells' novel The War of the Worlds (1898)<br>
has had an extraordinary influence on science fiction. <br>
Wells' Martians are a technologically advanced species<br>
with an ancient civilization. They somewhat resemble<br>
cephalopods, with large, bulky brown bodies and<br>
sixteen snake-like tentacles, in two groups of eight,<br>
around a quivering V-shaped mouth; they move around in<br>
100 feet tall tripod fighting-machines they assemble<br>
upon landing, killing everything in their path.<br>
<br>
<br>
<br>
by your name<br>
<br>
</body>
</html>

WHAT I NEED THE HTML FILE TO LOOK LIKE:

Are These Aliens Martians? //my output has
here

an adaptation

The men from Earth stared at the aliens. The little green men had pointed heads and orange toes with one long curly hair on each toe.
//Just need a
at the end of each paragraph
H. G. Wells' novel The War of the Worlds (1898) has had an extraordinary influence on science fiction. Wells' Martians are a technologically advanced species with an ancient civilization. They somewhat resemble cephalopods, with large, bulky brown bodies and sixteen snake-like tentacles, in two groups of eight, around a quivering V-shaped mouth; they move around in 100 feet tall tripod fighting-machines they assemble upon landing, killing everything in their path.
//Again just one
here not one each line


by your name

Edit 1: Updated code to get head and body tag.



Read more here: https://stackoverflow.com/questions/68477344/c-how-to-format-a-text-file-into-an-html-file

Content Attribution

This content was originally published by JK3 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: