www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/04/07/16:33:27

Date: Wed, 7 Apr 1999 16:33:14 -0400
Message-Id: <199904072033.QAA02417@envy.delorie.com>
From: DJ Delorie <dj AT delorie DOT com>
To: djgpp AT delorie DOT com
In-reply-to: <370BB323.E645FC7C@cityweb.de> (message from David Renz on Wed,
07 Apr 1999 21:33:55 +0200)
Subject: Re: DJGPP-Problem
References: <37026DF6 DOT BCAA8E59 AT cityweb DOT de> <Pine DOT HPP DOT 3 DOT 95 DOT 990401074240 DOT 16475A-100000 AT hepo DOT cc DOT lut DOT fi> <37090A8C DOT 89F098AA AT cityweb DOT de> <199904052126 DOT RAA09813 AT envy DOT delorie DOT com> <370BB323 DOT E645FC7C AT cityweb DOT de>
Reply-To: djgpp AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

> In TurboC you MUST do fflush(stdin) before you can use gets, because
> the buffer must be empty(How can you empty the buffer in DJGPP?).

I think you're solving the wrong problem.  You don't want to empty the
buffer, you just want to ignore the remainder of the line, right?  If
so, there are two solutions:

1. use scanf("%*[^\n]\n"); This will read and discard all characters
   up to and including the next newline (i.e. it ignores the rest of
   the line).

2. (My choice) Use fgets to read the line, then parse it with sscanf.

Consider the case where you redirect input from a file.  The "buffer"
is going to be the first 512 bytes of the file, *not* the first *line*
of the file.  fflush(stdin) would toss all 512 bytes, probably leaving
you in the middle of some line down the list.  The scanf solution
works for both cases.

Oh, and you should never use gets.  Always use fgets.  gets doesn't
have a way of stopping buffer overrun from long lines.  fgets is
safer.

- Raw text -


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