2014-03-13 36 views
1

我目前正在研究一個項目,在那裏我讀取一個文本文件,使其成爲html的正文代碼。 問題是每當有一個輸入/換行符時,我必須在其中輸入「
」。 而且......我不確定我是否能確定是否有新線。使用C++將一個txt轉換爲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 

感謝

回答

0

在你istream,關鍵是要一個char從流比較1310(取決於如果你是LF (ascii=10, found on UNIX-like systems)CRLF (ascii=13, ascii=10, found on Windows)的換行符

例如,由於ch是最近的char從您的istreamLF):

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

對於(CRLF),鑑於ch1是最新的,並且ch2是第二最新:

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

1310是回車和換行的值, 分別。


這就是它的全部。這聽起來像你可以做其他:)

+0

嘿藍色的冰。感謝您的回覆。 我只有一個小問題。如果用戶使用Windows並且文件中的文本文件的ASCII = 13,該怎麼辦? – Jarvis

+0

實際文字'ASCII = 13'?或者值'13'?如果它是值'13',那麼它是回車。 –

+0

我的意思是ASCII值13。 什麼是回車? – Jarvis