使用c ++将txt转换为html

时间:2014-03-13 04:12:08

标签: c++ html html5 istream

我目前正在开展一个项目,在那里我阅读了一个文本文件,使其成为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

由于

1 个答案:

答案 0 :(得分:0)

istream中,诀窍是将流中的char1310进行比较(取决于您是LF (ascii=10, found on UNIX-like systems)还是{{ 1}}用于换行。

例如,假设CRLF (ascii=13, ascii=10, found on Windows)chchar)中最新的istream

LF

对于(if(ch == 10) // insert <br> ),假设CRLF是最新的,ch1是第二个最新的:

ch2

if(ch1 == 10 && ch2 == 13) // insert <br> 13分别是回车和换行的值。


真的就是这一切。听起来你可以做其余的事情:))