Re: fgets() and embedded null characters
From: CBFalconer (cbfalconer_at_yahoo.com)
Date: 03/16/05
- Next message: Keith Thompson: "Re: contents of what a pointer points to??"
- Previous message: CBFalconer: "Re: CPU temperature reading with C"
- In reply to: David Mathog: "fgets() and embedded null characters"
- Next in thread: websnarf_at_gmail.com: "Re: fgets() and embedded null characters"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 16 Mar 2005 21:12:10 GMT
David Mathog wrote:
>
> 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)?
Because somebody wrote it that way about 30 years ago, and a change
would break all sorts of existing code.
>
> 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.
Bingo. Except you would be well advised to use getc rather than
fgetc. BTW, if your files have '\0' (nul, not null) chars in them,
they are not textfiles, and you will need to face the non-portable
treatment of line endings.
-- "If you want to post a followup via groups.google.com, don't use the broken "Reply" link at the bottom of the article. Click on "show options" at the top of the article, then click on the "Reply" at the bottom of the article headers." - Keith Thompson
- Next message: Keith Thompson: "Re: contents of what a pointer points to??"
- Previous message: CBFalconer: "Re: CPU temperature reading with C"
- In reply to: David Mathog: "fgets() and embedded null characters"
- Next in thread: websnarf_at_gmail.com: "Re: fgets() and embedded null characters"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|