delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/09/03/22:11:30

From: firewind <firewind AT metroid DOT dyn DOT ml DOT org>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: gets amd scanf
Date: 3 Sep 1997 22:27:53 GMT
Organization: Netcom
Lines: 23
Message-ID: <5uko99$lk2@dfw-ixnews11.ix.netcom.com>
References: <5ujllp$gu2$1 AT www DOT uno DOT edu>
NNTP-Posting-Host: elp-tx2-03.ix.netcom.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

aflcs AT uno DOT edu wrote:

> I am trying to read in a string with gets();

First, gets() is the antichrist. If you want to write decent programs, use
fgets() which allows you to specify a maximum length, thus ensuring your
buffer is not overrun. This is discussed in the comp.lang.c FAQ.

> This works fine if it appears before any other input statements,
> but if it appears after some scanf calls, then gets does not wait
> for any input.

IIRC, scanf() leaves the trailing newline in the input stream. The next time
gets() is called, it will read in this newline and instantly return. The
solution is to not intersparse different types of input functions. scanf() and
gets() do not get along. IMHO, the best solution is to read in an entire line
of text with fgets() and parse it with sscanf(). This is also discussed in
the comp.lang.c FAQ.

-- 
[- 				 firewind	                            -]
[-   email: firewind AT metroid DOT dyn DOT ml DOT org (home), firewind AT aurdev DOT com (work)  -]
[- 	    "You're just jealous because the voices talk to -me-."          -]

- Raw text -


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