使用c++将TXT文件转换为html文件

using c++ to convert a txt into html

本文关键字:文件 转换 html TXT c++ 使用      更新时间:2023-10-16

我目前正在做一个项目,我读取一个文本文件,使其成为html的主体代码。问题是每当有一个回车/换行符时,我必须输入"
"。和…我真的不确定我是如何判断是否有一个新的行。

下面是目前为止的代码:
#include <iostream>
#include <fstream>
#include <map>
using namespace std;
istream findParagraph(istream& is, string& word) {
    //if there's a new line here I need to make sure I add "<br >"
    //into is; then send it back to main
}
int main(int argc, char* argv[]) {
    argv[1] = "The Republic, by Plato.txt";
    ifstream infile(argv[1]);
    char ch = 0;

    ofstream out("title.html");
    out << "<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">" << endl
        << "<head>" << endl
        << "<meta http - equiv = "Content-Type" content = "text/html; charset=UTF-8" />" << endl
        << "<title>" << argv[1] << "</title>" << endl
        << "</head>" << endl 
        << "<body>" << endl;
    typedef map<string, unsigned> dictionary_type;
    dictionary_type words;
    string word;
    while (findParagraph(infile, word))
        ++words[word];
    out << "</body>" << endl << "</html>";
} //end main

谢谢

在您的istream中,技巧是将流中的char1310(取决于您是LF (ascii=10, found on UNIX-like systems)还是CRLF (ascii=13, ascii=10, found on Windows)的换行符)进行比较。

例如,假设chistream (LF)中最近的char:

if(ch == 10)
    // insert <br>

对于(CRLF),给定ch1是最新的,ch2是第二最新的:

if(ch1 == 10 && ch2 == 13)
    // insert <br>

1310分别为回车和换行的值。


这就是它的全部。听起来你可以做剩下的了