Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 can I remove it from being copied.

Here is my code attempt to do the same.

if (inputFile.is_open())
{
    {
        string currentLine;
        while (!inputFile.eof())
            while( getline( inputFile, currentLine ) )
            {
                string s1=currentLine;
                cout<<s1.length();
            }

[Updated Description] i have used notepad++ to determine the length of what i am selecting line by line. So they are showing some 123, 450, 500, 120 for which my program shows 124,451,501,120. Except for the last line, all line.length() shows an increased by 1 value.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.6k views
Welcome To Ask or Share your Answers For Others

1 Answer

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 by default, leaving the CR ( ) at the end of your string.

You'll need to trim the extraneous s. 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() == '
')
    {
        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(
        "
"
        "1
"
        "12
"
        "123
"
        "
"
        "1
"
        "12
"
        "123
"
        );

    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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...