www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/06/04/17:32:34

Xref: news2.mv.net comp.os.msdos.djgpp:4575
From: Broeker AT PROBLEM_WITH_INEWS_DOMAIN_FILE (Hans-Bernhard Broeker)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: fflush(stdin) : does it works ?
Date: 3 Jun 1996 13:48:41 GMT
Organization: RWTH -Aachen / Rechnerbetrieb Informatik
Lines: 31
Message-ID: <4ouqfp$cjs@news.rwth-aachen.de>
References: <4ohpaj$i0j AT tempo DOT univ-lyon1 DOT fr>
NNTP-Posting-Host: axpcl1.physik.rwth-aachen.de
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Dominique Micollet (dmicolet AT u-bourgogne DOT fr) wrote:
> I used to get a string with the following :


> {
> char One_String[80];
> ...
> scanf("%[^\n]",One_String);fflush(stdin);
> ...
> }

> The %[^\n] allows to catch any character excepted the new line and then to
> get strings with blanks, tabs and so on inside them.
> The fflush(stdin) is there to trash the new line, else the next scanf of a
> string does not works.

As others have already pointed out, fflush(stdin) will cause what
ANSI C calls 'undefined behaviour' (read the comp.lang.c FAQ about
the possible meanings of this term, if you haven't done so yet...).

For the present task, I think the correct solution would be this:
(caution: untested guess!)

scanf ("%[^\n]%*1[\n]", One_String);

The '%*1[\n]' should read exactly one newline, without storing it
anywhere. Another alternative might be:

scanf ("%[^\n]%*1c", One_String);

Hans-Bernhard Broeker (Aachen, Germany)

- Raw text -


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