Results 1 to 8 of 8
I have a problem with fflush(stdin) function in C, that when I am taking input from the user and through characterd variable from user it don't work like..
Code:
printf("No.:");
...
- 08-14-2005 #1Just Joined!
- Join Date
- Jul 2005
- Posts
- 17
problem with fflush(stdin) function
I have a problem with fflush(stdin) function in C, that when I am taking input from the user and through characterd variable from user it don't work like..
is not working. Please help me.Code:printf("No.:"); scanf("%d",&a); printf("Character"); scanf("%c",&ch);
- 08-15-2005 #2
You really should use fgets() combined with sscanf(), as scanf() messes up a lot when it comes to reading newlines, etc.
- 08-15-2005 #3
I havent touched C for a while but isnt fscanf("%s", &myBuff, stdin); the best thing to use for user intput? And should'nt you use getline() to get a line of input instead of fgets().
- 08-15-2005 #4The scanf() family of functions are notoriously bad for handling user input, so you read it into a buffer with fgets() and then do the conversion with sscanf(). getline() is part of the Readline library, and is considered generally more powerful than the fgets()-like line reading routines. However, if you are not going to use tab-completion or anything fancy, there's no point in really using the Readline library.
Originally Posted by Giro
- 08-15-2005 #5Linux Newbie
- Join Date
- Jul 2005
- Location
- Chd,India
- Posts
- 135
now that the problem is sorted out,guys could u tell me something about input buffering
i understand that commands like fgets and fputs have their stdio buffers
and read and write are system calls
but still,shoulnt output be the only one that causes buffering problems
i mean the whole idea of input buffering causing problems is vague(and new ) to meThe strong shall live and the weak will die
In the end,only the fittest survive in this world
- Shishio Makoto
- 08-15-2005 #6
As I understand it, if you did not buffer input, you would be forced to grab each character as it comes, and manipulate it directly, instead of waiting for a newline, and taking that input as a whole (sometimes the former is desirable, however.)
- 08-15-2005 #7Linux Newbie
- Join Date
- Oct 2004
- Posts
- 158
FWIW -
fflush(stdin) does not have defined behavior, according to the C standard, which gcc supports. So, you can't use it to clear an input buffer. Only output.
If you want to read a file, you cannot have a zero-length buffer. To change buffer size use setvbuf() for files that are open with FILE * descriptors. Performance goes down the tube wih really small buffers.
Buffering is required in both directions - input & output.
stdin doesn't have to be the keyboard - it could be a pipe.
Decent unix programs read either from stdin or from a pipe into stdin with no problem.
A good rule of thumb is to leave stdin and stdout alone, except for special applications. If you want to read keystrokes try changing tty properties to turn off canconical mode like this rather than messing up stdin:
Code:#include <termios.h> #include <unistd.h> #include <assert.h> #include <string.h> /*------------------------------------------------*/ int getch(void) { int c=0; struct termios org_opts, new_opts; int res=0; //----- store old settings ----------- res=tcgetattr(STDIN_FILENO, &org_opts); assert(res==0); //---- set new terminal parms -------- memcpy(&new_opts, &org_opts, sizeof(new_opts)); new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL); tcsetattr(STDIN_FILENO, TCSANOW, &new_opts); c=getchar(); //------ restore old settings --------- res=tcsetattr(STDIN_FILENO, TCSANOW, &org_opts); assert(res==0); return(c); }
- 07-05-2006 #8Just Joined!
- Join Date
- Dec 2005
- Location
- Assam, India
- Posts
- 36
Is there any way to flush the input buffer? The problem where I faced it will make it clear what I want exactly.
Since the scanf does not take in the newline when the enter key is pressed, it remains in the buffer which is taken by getchar(); thus it does not wait for user input anymore. Of course this problem can be solved by replacing scanf with functions such as gets() or similar function as follows:-Code:main(){ char buffer[20]; scanf(“%s”, buffer); getchar(); }
But I want to know if there is any way to flush the input buffer. fflush () works for output stream not input; aren’t there any equivalent function for input stream?Code:main(){ char buffer[20]; gets(buffer); getchar(); }


Reply With Quote
