Remove newline char at the end

This is a discussion on Remove newline char at the end within the C++ Programming forums, part of the General Programming Boards category; Im reading from a file with getline (file,line1); and i have a null character after every end of line. But ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    856

    Remove newline char at the end

    Im reading from a file with getline (file,line1); and i have a null character after every end of line.
    But i would like to append some string to it so i dont want the null character.

    How can we remove newline char in c++?

    Code:
    while(file.good())
    {
           getline (file,line1);
           str = "hello";
           str += line1;
           str += "world";
           cout << str << endl;
     }
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,803
    A null character and a newline character are two different things. Anyway, the solution is the same as what I just talked about recently in another thread: use getline() to control the loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    856
    Thank you Laserlight, i will search for your thread.

    By the way i resolved it with erase().
    Compiler MSVC++ 2013 with Code::Blocks.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    856
    I found your example, thanks.

    Actually to control the loop like this isnt enough for me cause i will still have newline char at the end of every line and when i append something it wont get printed on the same line.

    Erase() will remove them only it will remove the last character from the last line too because there is no more newline char there.

    So im wondering how could i check if its the last line already?

    Code:
    while(getline (file,line1))
    {
            if( not last line)
            line1.erase(line1.length()-1);
    }
    Last edited by Ducky; 12-23-2009 at 10:47 AM.
    Compiler MSVC++ 2013 with Code::Blocks.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,803
    This is what I mean:
    Code:
    while (getline(file, line1))
    {
        str = "hello";
        str += line1;
        str += "world";
        cout << str << endl;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    856
    Yes its not working that way.

    "world" is going to come always in the beginning of the phrase because of the newline char i guess.
    Compiler MSVC++ 2013 with Code::Blocks.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,803
    Quote Originally Posted by Ducky
    Yes its not working that way.
    For a moment I thought that I could have got it wrong, but a quick test shows that I am right.

    Quote Originally Posted by Ducky
    "world" is going to come always in the beginning of the phrase because of the newline char i guess.
    Post the smallest and simplest compilable program that demonstrates this effect using what I suggested. If by "i guess" you mean that you are guessing that this is what will happen, then you have guessed wrong. std::getline() extracts the delimiter but does not add it to the string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    856
    "hello" will get overwritten by "world" all the time.

    Code:
    #include <windows.h>
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main ()
    {
        string line, str;
        ifstream file;
        file.open("Dico.txt", ios::binary );
        while(getline (file,line))
        {
    
           str = "hello ";
           str += line;
           str += " world";
           cout << str << endl;
        }
        return 0;
    }
    I put three lines in Dico.txt.
    Compiler MSVC++ 2013 with Code::Blocks.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,803
    Since you are working with text, do not open the file in binary mode.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    856
    Thank you very much and Happy Holidays!
    Compiler MSVC++ 2013 with Code::Blocks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21