Clear standard input buffer for C program in Linux
Hello friends! i am writing a code in which i take inputs (numbers) from user and count the total number of positive, negative and zeros entered. I need to clear my standard input buffer before scanf() command. My compiler is completely ignoring the fflush(stdin) command. its not even showing any error!. I am using Linux(ubunto) platform. Help!
Last edited by Abhishek_kumar; 08-08-2014 at 02:27 PM..
It certainly is showing error but you're not looking for it. That's what scanf()'s return value is for, it returns 0 if it reads no values, 1 if it found 1 value, 2 for 2, etc. Each %... code counts as one "value".
The buffering thing is a known issue with scanf(). It's a "feature" -- it stops scanning at the first bad character -- which is pretty useless if you're not building a compiler.
fflush should work, if you tell it which stream to flush, which you are not. But the proper way to avoid these buffer problems is to not use scanf().
You can still use sscanf though, which is just as good without the problems! It scans a string, and has no buffer to leave things in.
Here's the usual way to do it:
Last edited by Corona688; 08-08-2014 at 12:18 PM..
Just started learning Unix and received my first assignment recently. We haven't learned many commands and honestly, I'm stumped. I'd like to receive assistance/guidance/hints.
1. The problem statement, all variables and given/known data:
How do I write a shell script that takes in a file or... (4 Replies)
Hello everyone,
Can someone please explain the input buffer behaviour for the read command in ksh93 on AIX?
I have 'googled' for weeks now, and did not find a satisfactory answer or solution to my dilemma.
I have the following code:
STTY=$(stty -g)
if ;then
stty -echo -icanon time 0 min... (1 Reply)
I'm running a ksh script that requires user interaction, and said users (myself among them) may get a little trigger happy and get ahead of ourselves and accidentally key in the wrong responses to future prompts in the script.
Naturally, I'd like to prevent that, so how does one clear that... (0 Replies)
Hi,
How can i clear the command. Suppose using esc k i retrieved teh command, but for some reason I want to clear and type fresh one... how can i do that.
thx (2 Replies)
I have a program R installed on Ubuntu under /usr/bin/R. I also have a different version installed under /home/user/R. I would like to invoke the locally installed program temporarily. How can I do this automatically by running a script and then switching to the default program?
thanks,
SM (2 Replies)
Hello,
I am having trouble clearing the serial port buffer using the iclear and iflush commands. The code runs without errors being returned, but when I check the buffer again there is still data.
The only way I have so far is to read until there is nothing left in the buffer. Shouldn't one... (1 Reply)
PROGRAM A <-> PROGRAM B
PROGRAM A sends data as STDIN ro PROGRAM B and when PROGRAM B is executed from PROGRAM A, it sends output back to PROGRAM A. This is implemented using 2 pipes (fd1 & fd2).
The above process happens in a loop and during the second run, the previous data that had been... (10 Replies)
how can i redirect standard input? i dont remember :/, though could you redirec not from a command? i mean, to redirect always stdin and stout (1 Reply)
When I worked with client-server (socket) programming, I encountered "the socket error# 10055" which means "No buffer space available". This might be a symptom of one or more applications that didn't return system resources (like memory) properly. Temporary solution was to reboot the machine to... (7 Replies)