delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/07/18:32:47

From: "A. Sinan Unur" <sinan DOT unur AT cornell DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Allegro -> possible bug
Date: Wed, 07 Jan 1998 17:23:39 -0500
Organization: Cornell University (http://www.cornell.edu/)
Lines: 62
Sender: asu1 AT cornell DOT edu (Verified)
Message-ID: <34B4006B.2F7E7C76@cornell.edu>
References: <34B39B82 DOT 4D55 AT oce DOT nl>
NNTP-Posting-Host: cu-dialup-0002.cit.cornell.edu
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019