1

I'm trying to write a program that converts a txt file to HTML. Here is part of my code:

while (getline(text, lineInFile)) {
    input.clear();
    //input << lineInFile;
    input.str(lineInFile);
    input >> convert;
    if(convert == "1"){
        if (ul == true) {
            html << "</ul>\n";
            ul = false;
        }
        else if(ol == true){
            html << "</ol>\n";
            ol = false;
        }
        elseOr = true;
        lineInFile.erase(lineInFile.begin() + 0, lineInFile.begin() + 1);
        html << "<h1>" << lineInFile << " </h1>\n";
    }

What I wanted in my HTML file was something like:

<h1> Hello </h1>

But instead what I'm getting is:

<h1> Hello 

</h1>

I don't want the newline at the end there. How can I get rid of it?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Thomas Lau
  • 19
  • 2
  • 3
    [Please do not post images of code because they are hard to use.](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) Code should be posted directly **as text** in your question. – MikeCAT Nov 07 '20 at 00:02
  • 3
    Given the code shown, the ONLY way it can generate this output is if there is a trailing `\r` character present in `lineInFile` that `std::getline()` did not strip off. That typically happens when running code on a non-Windows machine, but reading files that have Windows-style CRLF line breaks in them. On non-Windows machines, `std::getline()` typically recognizes only bare-LF line breaks, not CRLF line breaks. But on Windows machines, `std::getline()` typically recognizes both styles. – Remy Lebeau Nov 07 '20 at 00:05
  • @paulsm4 It does stop on a LF new line character, yes. And does not output the LF. But, if there is a CR carriage return character in front of the LF, the behavior is implementation-defined whether the CR gets discarded or not. On Windows, it usually is. On non-Wnidows, it is usually not. – Remy Lebeau Nov 07 '20 at 00:13
  • The standard C library function [fgets()](https://linux.die.net/man/3/fgets) retains the newline, and you have to manually delete it. [std::getline( )](https://en.cppreference.com/w/cpp/string/basic_string/getline), however, should stop the read operation once the new-line character is found. It sounds like maybe this is a Windows `\r\n` vs. Linux `\n` LF problem. See this thread for more details: https://stackoverflow.com/a/45957854/421195 – paulsm4 Nov 07 '20 at 00:18

0 Answers0