Re: istream::getline

Discussion in 'C++' started by Victor Bazarov, Aug 13, 2003.

  1. "s" <> wrote...
    > I using this function to read in lines from a text file. The file is
    > delimited by CR/LF. The buffer that get copied contains a LF at the end
    > of the string. It looks like getline is discarding the CR but not the
    > LF. Is there anyway to get it to discard both the CR/LF? I'm using
    > linux OS.


    I think you got it wrong. std::getline puts everything, but discards
    the line delimiter. If you didn't change it, the delimiter is \n (or
    LF as you know it). What you get is the CR (\r) in your string. You
    just need to remove it:

    std::getline(std::cin, line);
    if (line[line.length()-1] == '\r') // there is a \r in my line!
    line.erase(line.length()-1); // throw it away

    Victor
     
    Victor Bazarov, Aug 13, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Todd Beauchemin

    overloaded >> and istream delimiters

    Todd Beauchemin, Jul 31, 2003, in forum: C++
    Replies:
    1
    Views:
    453
    Victor Bazarov
    Jul 31, 2003
  2. Thomas Matthews

    istream in init list

    Thomas Matthews, Sep 19, 2003, in forum: C++
    Replies:
    4
    Views:
    580
    Kevin Goodsell
    Sep 20, 2003
  3. Morten Rodal

    g++ istream::readsome trouble

    Morten Rodal, Apr 19, 2004, in forum: C++
    Replies:
    3
    Views:
    3,736
    Jeff Schwab
    Apr 19, 2004
  4. Steve
    Replies:
    6
    Views:
    3,703
    Steve
    May 13, 2004
  5. xmllmx
    Replies:
    5
    Views:
    603
    Jorgen Grahn
    Jun 15, 2010
Loading...

Share This Page