fgets and newline

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Mimic

    fgets and newline

    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
  • Jacques Labuschagne

    #2
    Re: fgets and newline

    Mike Mimic wrote:[color=blue]
    > 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[/color]

    std::getline?


    Jacques.

    Comment

    • Mike Mimic

      #3
      Re: fgets and newline

      Hi!

      Jacques Labuschagne wrote:[color=blue]
      > Mike Mimic wrote:[color=green]
      >> 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).[/color]
      >
      > std::getline?[/color]

      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

      Comment

      • Jacques Labuschagne

        #4
        Re: fgets and newline

        Mike Mimic wrote:[color=blue]
        >
        > 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.[/color]

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

        getline(myfile, mystring, '\n');
        if (*mystring.rbeg in() == '\r'){
        *mystring.rbegi n() = ' ';
        // or you could actually call mystring.erase( )
        }
        [color=blue]
        >
        > And how can I convert file descriptor to a C++ stream (as fdopen)?[/color]

        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

        Comment

        • John Harrison

          #5
          Re: fgets and newline


          "Mike Mimic" <ppagee@yahoo.c om> wrote in message
          news:c83o1a$h27 $1@planja.arnes .si...[color=blue]
          > Hi!
          >
          > Is there a way to "say" to fgets to use any of \n, \r, \r\n end line
          > markers as line separators?[/color]

          No, write your own function to do this. Simple enough I think.
          [color=blue]
          >
          > Is there some other library function which would read line by line (end
          > line markers do not need to be included).[/color]

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

          john


          Comment

          Working...