我目前正在开展一个项目,在那里我阅读了一个文本文件,使其成为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 :(得分:0)
在istream
中,诀窍是将流中的char
与13
或10
进行比较(取决于您是LF (ascii=10, found on UNIX-like systems)
还是{{ 1}}用于换行。
例如,假设CRLF (ascii=13, ascii=10, found on Windows)
是ch
(char
)中最新的istream
:
LF
对于(if(ch == 10)
// insert <br>
),假设CRLF
是最新的,ch1
是第二个最新的:
ch2
if(ch1 == 10 && ch2 == 13)
// insert <br>
和13
分别是回车和换行的值。
真的就是这一切。听起来你可以做其余的事情:))