Question

I'm currently working on a project where I read a textfile to make it into a body code of html. The problem is whenever there is an enter/newline I have to enter "
" into it. And...I'm not really sure how I could tell if there is a new line.

Here's my code so far:

#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

Thanks

Was it helpful?

Solution

In your istream, the trick is to compare a char from the stream to 13 or 10 (depending if you are LF (ascii=10, found on UNIX-like systems) or CRLF (ascii=13, ascii=10, found on Windows) for newlines.

For example, given that ch is the most recent char from your istream (LF):

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

For (CRLF), given that ch1 is the newest, and ch2 is the second newest:

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

13 and 10 are the values for Carriage Return and Line Feed, respectively.


That's really all there is to it. It sounds like you can do the rest :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top