www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/07/11/11:30:08

Xref: news2.mv.net comp.os.msdos.djgpp:5866
From: lehmann AT mathematik DOT th-darmstadt DOT de (Alexander Lehmann)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Beating fflush to death
Date: 11 Jul 1996 13:56:57 GMT
Organization: Technische Hochschule Darmstadt
Lines: 37
Message-ID: <4s3179$j6t@rs18.hrz.th-darmstadt.de>
References: <31E423F2 DOT 5386 AT panix DOT com>
NNTP-Posting-Host: fb0432.mathematik.th-darmstadt.de
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Norbert Jay (norbertj AT panix DOT com) wrote:
: My how to write C books says that SOMETIMES scanf leaves a newline 
: character behind in the buffer.  The book suggests fflush(stdin) which as 
: we all know does not work.  I use gets(dum) (char dum[1]) to clear the 
: buffer after each scanf fpr this particular program.  

: 1. Will this work on ALL versions of DOS?  If so, why is something like 
: it built into the lib?

gets doesn't check for buffer overruns, so you may overwrite some
other part of your data with this call, if there is more than a single
newline character available. (Remember the Internet worm, that
exploited a similar bug in fingerd to propagate), if you want to use
gets, don't , use fgets instead, it has a buffer size parameter and is
safer.

: 3. Browsing that great mail archive in delorie's home page confused me 
: even more what with scanf("%*[^\n]\n") which I could not get to work.

Hm, that should scan a string containing everything except a newline
and throw it away and then also the next newline, looks fine to me.

: 4. Is the inconsistency between systems like DOS - UNIX or is it internal 
: to each system

Well, scanf isn't really a function that should be used at all IMHO, I
prefer fgets and fscanf, at least you get a chance to examine the
string in case of an error.


bye, Alexander

--
Alexander Lehmann,                                  |  "On the Internet,
alex AT hal DOT rhein-main DOT de  (plain, MIME, NeXT)         |   nobody knows
lehmann AT mathematik DOT th-darmstadt DOT de (plain)          |   you're a dog."
!!CHANGED!! <URL:http://www.mathematik.th-darmstadt.de/~lehmann/>

- Raw text -


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