Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7170931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:18:38+00:00 2026-05-28T15:18:38+00:00

I am reading a file line by line and adding each line to a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:18:39+00:00Added an answer on May 28, 2026 at 3:18 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When reading lines from a text file using python, the end-line character often needs
I'm running a while loop reading each line in a file, and then fork
I have a file have multiple lines. For each line the format is String
I am trying to replace text in a text file by reading each line,
I'm reading a file using bufferedreader, so lets say i have line = br.readLine();
Other than reading the GetFileName() file and reading to line GetFileLineNumber() of a stackframe
I'm reading a .csv file that was created in Excel with the first line
My simple requirement: Reading a huge (> a million) line test file (For this
I have a CSV file. Each line is made up of the same format
I want to iterate over each line of an entire file. One way to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.