Mail Archives: djgpp/1998/01/07/18:32:47
A. Jans-Beken wrote:
>
> I have noticed a difference between the allegro 2.2 function:
> pack_fgets(...)
> and the standard stdio function:
> fgets(...)
>
> It seems that the standard function reads a string INCLUDING the '\n'
> at the end while the packed function reads a string WITHOUT the
> trailing '\n'.
From the ANSI standard (quoted from Plauger's The Standard C Library)
7.9.7.2 The fgets function
...
Description
The fgets function reads at most one less than the number of
characters specified by n from the stream pointed to by stream into the
array pointed to by s. No additional characters are read after a
new-line character (which is retained) or after end-of-file. A null
character is written immediately after the last character into the
array.
>
> In stdio code this means:
> int main() {
> ...
> fputs("hello\n", f);
> ...
> fgets(buffer,100,f);
> *(buffer + strlen(buffer) - 1) = '\0';
this is unnecessary; see the description above.
> ...
> }
>
> versus the allegro code:
> int main() {
> ...
> pack_fputs("hello\n", f);
> ...
> pack_fgets(buffer,100,f);
> // String is already terminated by '\0'
> ...
> }
what you claim initially and what you seem to point out with the comment
above don't match each other. since the allegro documentation claims
this function should work like its stdio equivalent, if it does not read
a newline that occurs before the 100th character (and stop reading
there), this is indeed a bug. however, ANSI guarantees that the string
read in by fgets will be null-terminated so i don't understand your
objection above.
--
----------------------------------------------------------------------
A. Sinan Unur
Department of Policy Analysis and Management, College of Human Ecology,
Cornell University, Ithaca, NY 14853, USA
mailto:sinan DOT unur AT cornell DOT edu
http://www.people.cornell.edu/pages/asu1/
- Raw text -