fgets() and embedded null characters

From: David Mathog (mathog_at_caltech.edu)
Date: 03/16/05


Date: Wed, 16 Mar 2005 10:30:07 -0800

Every so often one of my fgets() based programs encounters
an input file containing embedded nulls. fgets is happy to
read these but the embedded nulls subsequently cause problems
elsewhere in the program. Since fgets() doesn't return
the number of characters read it is pretty tough to handle
the embedded nulls once they are in the buffer.

So two questions:

1. Why did the folks who wrote fgets() have a successful
read return a pointer to the storage buffer (which the
calling routine already knew in any case) instead of the
number of characters read (which often cannot determine at
all after the fact if there are embedded nulls in the input)?

2. Can somebody please supply a pointer to a function
written in ANSI C that:

   A) reads from a stream (like fgets)
   B) stores to a preallocated buffer (like fgets)
   C) accepts the size of the buffer (like fgets)
   D) returns the number of characters read (unlike fgets)
   E) sets read status, ideally in an integer combining
       status bits more or less like these:
          1 EOF
          2 LINETOOBIG (instead of having to check the last byte)
          4 READERROR (any other kind of READ error)
       (read status = 1 with a nonzero returned length would
        not be an error, it just indicates that all input data
        has been consumed.)

If need be I can roll my own from fgetc, but I'd rather not reinvent
this wheel.

Thanks,

David Mathog
mathog@caltech.edu



Relevant Pages

  • system() and _flushall()
    ... You must explicitly flush (using fflush or _flushall) or close any stream ... However, on the next read from the input file using fgets, I get ... forgotten that there's still data in the buffer (the buffer is not destroyed ...
    (microsoft.public.vc.language)
  • Re: system() and _flushall()
    ... > You must explicitly flush (using fflush or _flushall) or close any stream ... However, on the next read from the input file using fgets, I get ... > forgotten that there's still data in the buffer (the buffer is not destroyed ...
    (microsoft.public.vc.language)
  • Re: fgets() and embedded null characters
    ... > Every so often one of my fgets() based programs encounters ... > an input file containing embedded nulls. ... > the embedded nulls once they are in the buffer. ...
    (comp.lang.c)
  • Re: fgets() and embedded null characters
    ... > Every so often one of my fgets() based programs encounters ... > an input file containing embedded nulls. ... > the embedded nulls once they are in the buffer. ... The C language designers were "hackers". ...
    (comp.lang.c)
  • Re: fgets() and embedded null characters
    ... > Every so often one of my fgets() based programs encounters ... > an input file containing embedded nulls. ... > the embedded nulls once they are in the buffer. ... suitable for reading with a text stream. ...
    (comp.lang.c)