Mail Archives: djgpp/1996/07/15/14:17:45
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 -