微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C++如何将文本文件格式化为html文件

如何解决C++如何将文本文件格式化为html文件

我对 C++ 很陌生。我有我需要我的程序做的构建块,即:读取由命令行输入的文本文件(例如 ./textToHtml.exe Alien.txt),并通过实现正确的 HTML 标签从文本文件创建一个 html 文件,当必要的。

我提供了下面的代码,以及 html 文件结构。这是一个项目,我需要在每个段落和每行空白行之后都有换行符 <br>。我已经提供了我想要的最后一个 HTML 结构。

请注意,我确定我有一些不必要的行或冗余代码

#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;
}

HTML 文件如下所示:

<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>

我需要 HTML 文件的样子:

<HTML>
<head>
<title>Are These Aliens Martians?</title> //my output has <br> here
<br>
<br>
an adaptation<br>
<br>
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.<br> //Just need a <br> at the end of each paragraph
<br>
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,bulky brown bodies and
sixteen snake-like tentacles,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.<br> //Again just one <br> here not one each line
<br>
<br>
<br>
by your name<br>
<br>
</body>
</html>

解决方法

尝试更像这样的事情:

#include <iostream>
#include <string>
#include <fstream>

void htmlencode(std::string &s)
{
    std::string::size_type pos = 0;
    while ((pos = s.find_first_of("<>&",pos)) != std::string::npos)
    {
        std::string replacement;
        switch (s[pos])
        {
            case '<':
                replacement = "&lt;";
                break;
            case '>':
                replacement = "&gt;";
                break;
            case '&':
                replacement = "&amp;";
                break;
        }
        s.replace(pos,1,replacement);
        pos += replacement.size();
    }
}

int main(int argc,char* argv[])
{
    std::string fn = argv[1];

    std::ifstream txtFile(fn);
    if (txtFile)
    {
        std::string fileName = fn.substr(0,fn.rfind('.'));

        std::ofstream html(fileName + "1.html");
        if (html)
        {
            html << "<HTML>\n"
                << "<head>\n"
                << "<title>";
            std::string line;
            if (std::getline(txtFile,line))
            {
                htmlencode(line);
                html << line;
            }
            html << "</title>\n"
                << "</head>\n"
                << "<body>\n";
            bool lastLineNotEmpty = false;
            while (std::getline(txtFile,line))
            {
                if (line.empty())
                {
                    if (lastLineNotEmpty)
                        html << "<br>\n";
                    html << "<br>\n";
                    lastLineNotEmpty = false;
                }
                else
                {
                    if (lastLineNotEmpty)
                        html << '\n';
                    htmlencode(line);
                    html << line;
                    lastLineNotEmpty = true;
                }
            }
            if (lastLineNotEmpty)
                html << "<br>\n";
            html << "</body>\n"
                << "</html>\n";
        }
    }
    return 0;
}

Online Demo

然而,HTML 有专门为段落设计的 <p></p> 标签,所以你应该考虑使用它们而不是 <br>,例如:

#include <iostream>
#include <string>
#include <fstream>
     
void htmlencode(std::string &s)
{
    std::string::size_type pos = 0;
    while ((pos = s.find_first_of("<>&",replacement);
        pos += replacement.size();
    }
}
     
int main(int argc,fn.rfind('.'));
     
        std::ofstream html(fileName + "1.html");
        if (html)
        {
            html << "<HTML>\n"
                << "<head>\n"
                << "<title>";
            std::string line;
            if (std::getline(txtFile,line))
            {
                htmlencode(line);
                html << line;
            }
            html << "</title>\n"
                << "</head>\n"
                << "<body>\n";
            bool inParagraph = false;
            while (std::getline(txtFile,line))
            {
                if (line.empty())
                {
                    if (inParagraph)
                    {
                        inParagraph = false;
                        html << "</p>\n";
                    }
                }
                else
                {
                    if (!inParagraph)
                    {
                        inParagraph = true;
                        html << "<p>\n";
                    }
                    htmlencode(line);
                    html << line << '\n';
                }
            }
            if (inParagraph)
                html << "</p>\n";
            html << "</body>\n"
                << "</html>\n";
        }
    }
    return 0;
}

Online Demo

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。