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. Advertisements

  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. Advertisements

  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. Advertisements

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. Replies:
    6
    Views:
    1,468
    Mark McIntyre
    Jun 25, 2003
  2. Eigenvector

    fgets and problems reading into array

    Eigenvector, Jul 26, 2003, in forum: C Programming
    Replies:
    12
    Views:
    801
    Glen Herrmannsfeldt
    Jul 29, 2003
  3. Charles Erwin
    Replies:
    4
    Views:
    768
    Joe Wright
    Oct 6, 2003
  4. Eirik

    fgets() and scanf() peculiarity

    Eirik, Dec 10, 2003, in forum: C Programming
    Replies:
    6
    Views:
    1,361
    Richard Heathfield
    Dec 12, 2003
  5. RSoIsCaIrLiIoA

    on fgets and sscanf and their bugs

    RSoIsCaIrLiIoA, Apr 20, 2006, in forum: C Programming
    Replies:
    7
    Views:
    1,741
    Richard Heathfield
    Apr 25, 2006
  6. Justme
    Replies:
    9
    Views:
    837
    clayne
    Oct 1, 2006
  7. Replies:
    16
    Views:
    2,828
    Keith Thompson
    Nov 28, 2006
  8. dave
    Replies:
    4
    Views:
    783
Loading...