clearing input streams 
Author Message
 clearing input streams

Quote:

>Is there a portable way to determine how many characters are in the
>buffer of an input stream?

No.

Quote:
> I checked all the library functions, and
>none of them seem to do this.  However it looks like ftell(stdin)

On a seekable stream (such as taking input froma file that tells you
the position in the file, not how much data there is remaining). On a
nn-seekable stream it will probably fail.

Quote:
>or sizeof(stdin) just might do this.

sizeof gives you the size of a type. So sizeof(stdin) evaluates to the
compile time constant sizeof(FILE *)

Quote:
> (I doubt it.)  So I'm assuming
>there is no library function.  But would it be possible to write a
>function to do this?

Not portably. There may well be platforms where this information is simply
not available (subject to what you mean by ``the input buffer'').

Quote:
> The idea of this is so I can clear the input
>stream more easily.

Why do you want to? Clearing the input stream is usually a bad idea.

Quote:
>Or would it be better to insert a rarely used
>character into the end of the stream, and then clear until it is
>reachead?  (I'm just now getting around to correcting the fflush(stdin)
>used throughout my programs.)

Decode on the form of input your program takes. Usually input takes the
form of logical units or records. The form of these will usually indicate
where you need to read up to. The most common format in C is new-line
terminated lines so you simply use fgets() to read in a whole line
or use sone other mechanism to read up to the new-line character.

Consider this. If, as a user, I type ahead it is rather more often than
not because I *want* to type ahead. Programs that discard my input for
no good reason tend to get dumped very quickly.

--
-----------------------------------------


-----------------------------------------



Thu, 21 Dec 2000 03:00:00 GMT  
 clearing input streams

Quote:

> Consider this. If, as a user, I type ahead it is rather more often than
> not because I *want* to type ahead. Programs that discard my input for
> no good reason tend to get dumped very quickly.

This is a very good point!  But now I have to correct my other bad
habit.  scanf().  I am somewhat confused on how to use the more proper
functions.  Now, without being able to fflush(stdin) I don't know how
to use scanf() either.  Now I am basically without input routines which
I know!  BTW, fflush(stdin) does work on my system, but I am making a
point of keeping my code portable, since I am trying to learn pure
ANSI C, and then let the rest come later, if needed.

--

"He kept his operators covert, except for his continual ad campaign to
convince
people that Hell was in fact a fun place.  No one with any sense
believed that
-- but there were a lot of stupid people in the world. --Piers Anthony"



Thu, 21 Dec 2000 03:00:00 GMT  
 clearing input streams

Quote:

> This is a very good point!  But now I have to correct my other bad
> habit.  scanf().  I am somewhat confused on how to use the more proper
> functions.  Now, without being able to fflush(stdin) I don't know how
> to use scanf() either.  Now I am basically without input routines which
> I know!  

The perennial solution to using a finicky function such as 'scanf()' is to
call it in an error-checking loop, terminating when matching input is
finally found. However, this approach is far from error-free, and can
easily get you caught in an infinite loop. See the FAQ for more details.

The -real- solution, of course, is to use 'fgets().' It is not a hard
function to master. (The conversion of input strings to the desired end
type, such as 'float' or 'int' is a little trickier, however.)

Quote:
> BTW, fflush(stdin) does work on my system, but I am making a
> point of keeping my code portable, since I am trying to learn pure
> ANSI C, and then let the rest come later, if needed.

Admirable.

--
(initiator of the campaign for grumpiness where grumpiness is due in c.l.c)

This article is copyright 1998 by firewind. It is licensed under the
OpenContent license (http://www.opencontent.org/opl.txt).



Thu, 21 Dec 2000 03:00:00 GMT  
 clearing input streams

Quote:


>> Consider this. If, as a user, I type ahead it is rather more often than
>> not because I *want* to type ahead. Programs that discard my input for
>> no good reason tend to get dumped very quickly.

>This is a very good point!  But now I have to correct my other bad
>habit.  scanf().  I am somewhat confused on how to use the more proper
>functions.  Now, without being able to fflush(stdin) I don't know how
>to use scanf() either.  Now I am basically without input routines which
>I know!  BTW, fflush(stdin) does work on my system, but I am making a
>point of keeping my code portable, since I am trying to learn pure
>ANSI C, and then let the rest come later, if needed.

The normal approach for line based input is to read a line using fgets()
which you can then puck apart with various functions such as strtok(),
atoi() strtol() and not least sscanf(). This is explained in more detail
in section 12.20 of the FAQ.

--
-----------------------------------------


-----------------------------------------



Fri, 22 Dec 2000 03:00:00 GMT  
 clearing input streams


|> functions.  Now, without being able to fflush(stdin) I don't know how
|> to use scanf() either.  Now I am basically without input routines which
|> I know!  BTW, fflush(stdin) does work on my system, but I am making a

use fgets() for line-buffered input, then parse the buffer read using
strtok() and friends.



Fri, 22 Dec 2000 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. How to clear input stream in ANSI C?

2. string stream clear

3. clear characters in a stream-buffer

4. clear the input buffer?

5. HELP: clearing input buffer in c

6. Unable to sync input and output streams

7. c file (stream) input buffer problem help PLEASE!

8. flushing the standard input stream (newb question)

9. ANN: Numeric input from file streams

10. state of input stream after bad scanf ?5

11. Problem with input stream redirection in lex

12. newbie question about input stream buffer

 

 
Powered by phpBB® Forum Software