fgets and newline

Discussion in 'C++' started by Mike Mimic, May 15, 2004.

  1. Mike Mimic

    Mike Mimic Guest

    Hi!

    Is there a way to "say" to fgets to use any of \n, \r, \r\n end line
    markers as line separators?

    Is there some other library function which would read line by line (end
    line markers do not need to be included).


    Mike
    Mike Mimic, May 15, 2004
    #1
    1. Advertising

  2. Mike Mimic wrote:
    > Hi!
    >
    > Is there a way to "say" to fgets to use any of \n, \r, \r\n end line
    > markers as line separators?
    >
    > Is there some other library function which would read line by line (end
    > line markers do not need to be included).
    >
    >
    > Mike


    std::getline?


    Jacques.
    Jacques Labuschagne, May 15, 2004
    #2
    1. Advertising

  3. Mike Mimic

    Mike Mimic Guest

    Hi!

    Jacques Labuschagne wrote:
    > Mike Mimic wrote:
    >> Is there a way to "say" to fgets to use any of \n, \r, \r\n end line
    >> markers as line separators?
    >>
    >> Is there some other library function which would read line by line
    >> (end line markers do not need to be included).

    >
    > std::getline?


    I missed that.

    But if line is terminated with \r\n that I will have to call getline
    twice for every line as I will get empty line after \r.

    And how can I convert file descriptor to a C++ stream (as fdopen)?


    Mike
    Mike Mimic, May 15, 2004
    #3
  4. Mike Mimic wrote:
    >
    > I missed that.
    >
    > But if line is terminated with \r\n that I will have to call getline
    > twice for every line as I will get empty line after \r.


    Not if you use \n as your delimiter and just erase the \r by hand.
    Something like:

    getline(myfile, mystring, '\n');
    if (*mystring.rbegin() == '\r'){
    *mystring.rbegin() = ' ';
    // or you could actually call mystring.erase()
    }

    >
    > And how can I convert file descriptor to a C++ stream (as fdopen)?


    fdopen() is not part of standard C or C++. If you're doing platform
    specific stuff you may need to write your own getline() equivalent.


    Jacques
    Jacques Labuschagne, May 15, 2004
    #4
  5. "Mike Mimic" <> wrote in message
    news:c83o1a$h27$...
    > Hi!
    >
    > Is there a way to "say" to fgets to use any of \n, \r, \r\n end line
    > markers as line separators?


    No, write your own function to do this. Simple enough I think.

    >
    > Is there some other library function which would read line by line (end
    > line markers do not need to be included).


    std::getline, but again doesn't do the processing on end of lines you
    require.

    john
    John Harrison, May 15, 2004
    #5
    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. Charles Erwin
    Replies:
    4
    Views:
    570
    Joe Wright
    Oct 6, 2003
  2. RSoIsCaIrLiIoA

    on fgets and sscanf and their bugs

    RSoIsCaIrLiIoA, Apr 20, 2006, in forum: C Programming
    Replies:
    7
    Views:
    1,542
    Richard Heathfield
    Apr 25, 2006
  3. Justme
    Replies:
    9
    Views:
    610
    clayne
    Oct 1, 2006
  4. Replies:
    16
    Views:
    2,345
    Keith Thompson
    Nov 28, 2006
  5. dave
    Replies:
    4
    Views:
    664
Loading...

Share This Page