Getline to String Copies newline as well

前端 未结 2 858
旧时难觅i
旧时难觅i 2020-12-19 20:07

I am reading a file line by line and adding each line to a string. However the string length increases by 1 for every line which I believe is due to newline character. How c

相关标签:
2条回答
  • 2020-12-19 20:29

    That's because you're under MS-Windows and they add a "\r" before the "\n" and that "\r" is not removed.

    0 讨论(0)
  • 2020-12-19 20:40

    It looks like inputFile has Windows-style line-breaks (CRLF) but your program is splitting the input on Unix-like line-breaks (LF), because std::getline(), breaks on \n by default, leaving the CR (\r) at the end of your string.

    You'll need to trim the extraneous \rs. Here is one way to do it, along with a small test:

    #include <iostream>
    #include <sstream>
    #include <iomanip>
    
    void remove_carriage_return(std::string& line)
    {
        if (*line.rbegin() == '\r')
        {
            line.erase(line.length() - 1);
        }
    }
    
    void find_line_lengths(std::istream& inputFile, std::ostream& output)
    {
        std::string currentLine;
        while (std::getline(inputFile, currentLine))
        {
            remove_carriage_return(currentLine);
            output
                << "The current line is "
                << currentLine.length()
                << " characters long and ends with '0x"
                << std::setw(2) << std::setfill('0') << std::hex
                << static_cast<int>(*currentLine.rbegin())
                << "'"
                << std::endl;
        }
    }
    
    int main()
    {
        std::istringstream test_data(
            "\n"
            "1\n"
            "12\n"
            "123\n"
            "\r\n"
            "1\r\n"
            "12\r\n"
            "123\r\n"
            );
    
        find_line_lengths(test_data, std::cout);
    }
    

    Output:

    The current line is 0 characters long and ends with '0x00'
    The current line is 1 characters long and ends with '0x31'
    The current line is 2 characters long and ends with '0x32'
    The current line is 3 characters long and ends with '0x33'
    The current line is 0 characters long and ends with '0x00'
    The current line is 1 characters long and ends with '0x31'
    The current line is 2 characters long and ends with '0x32'
    The current line is 3 characters long and ends with '0x33'
    

    Things to note:

    • You don't need to test for EOF. std::getline() will return the stream, which will cast to false when it can read no more from inputFile.
    • You don't need to copy a string to determine its length.
    0 讨论(0)
提交回复
热议问题