8 Replies - 8572 Views - Last Post: 23 June 2009 - 12:00 PM Rate Topic: -----

#1 Master Jake  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 106
  • Joined: 27-February 09

C - Flushing The Input Buffer

Posted 22 June 2009 - 07:32 PM

I'm not exactly sure if this is the correct terminology, but does anyone know how to simply flush the input buffer.

Here is what I mean by this. I have a program that is run through command-line that types out the arguments you pass into it letter by letter using a Sleep() delay of whatever they specify. The problem is, they can randomly mash on the keyboard while it's typing, and when it is complete, all the keys they pressed show up in the next input stream.

Example:

C:\>edc 50 "Type this text onto the screen"
Type this text onto

now as that text is typing out, they randomly press "asdfjlkwijueokjflks". Now when it is done typing and runs the input stream, it will look like this

Type this text onto the screen
Enter Some Text: asdfjlkwijueokjflks

when I actually want it to be empty. So I need to flush it, or whatever. This is also a problem because if they press enter during the typing, it will completely skip the next input stream.

Also, I prefer if we stick to C only, so now <iostream> bullshit, if you know what I mean. I'm just looking for a very simple way to clear the input buffer after the text is typed out.

Thanks for your help.

Is This A Good Question/Topic? 0
  • +

Replies To: C - Flushing The Input Buffer

#2 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5394
  • View blog
  • Posts: 27,389
  • Joined: 10-May 07

Re: C - Flushing The Input Buffer

Posted 22 June 2009 - 07:35 PM

Assuming that your input functionality is in it's own function, couldn't you sprinf("\0") to the variable containing the input, therefor clearing it out?
Was This Post Helpful? 0
  • +
  • -

#3 Dantheman  Icon User is offline

  • D.I.C Regular

Reputation: 34
  • View blog
  • Posts: 445
  • Joined: 27-May 09

Re: C - Flushing The Input Buffer

Posted 22 June 2009 - 07:45 PM

I always flush the input buffer with this loop:

while ((ch = getchar()) != '\n' && ch != EOF)
continue;


Was This Post Helpful? 0
  • +
  • -

#4 Master Jake  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 106
  • Joined: 27-February 09

Re: C - Flushing The Input Buffer

Posted 22 June 2009 - 07:55 PM

Well, the C command-line program is ran in a batch file. Batch is setting a variable after it runs:


For Example:
set /p var=Enter Text:

I also don't want the "Enter Text:" part to disappear, I just want anything they may have spam typed during the delay to disappear leaving it with a fresh

Enter Text:

prompt after the delay. Where exactly would I put that while loop in order to do this, here is my code:


#include <stdio.h>
#include <windows.h>

int main(int argc, char *argv[])
{
		int i, x, argToStart;
		char ch;
		HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
		
		if (argc > 2)
		{
			
		if (!strcmp(argv[2], "-n"))
			argToStart = 3;
		else
			argToStart = 2;
		
		for (i = argToStart; i < argc; i++)
		{
				for (x = 0; x < strlen(argv[i]); x++)
				{
					if (argv[i][x] == '/' && argv[i][x + 1] == '*')
					{
						switch (argv[i][x+2])
						{
							case 'z':
								SetConsoleTextAttribute(hConsole, 0);
								break;
								
							case 'b':
								SetConsoleTextAttribute(hConsole, 1);
								break;
								
							case 'g':
								SetConsoleTextAttribute(hConsole, 2);
								break;
								
							case 'c':
								SetConsoleTextAttribute(hConsole, 3);
								break;
								
							case 'r':
								SetConsoleTextAttribute(hConsole, 4);
								break;
								
							case 'm':
								SetConsoleTextAttribute(hConsole, 5);
								break;
								
							case 'y':
								SetConsoleTextAttribute(hConsole, 6);
								break;
								
							case 'D':
								SetConsoleTextAttribute(hConsole, 7);
								break;
								
							case 'd':
								SetConsoleTextAttribute(hConsole, 8);
								break;
								
							case 'B':
								SetConsoleTextAttribute(hConsole, 9);
								break;
								
							case 'G':
								SetConsoleTextAttribute(hConsole, 10);
								break;
								
							case 'C':
								SetConsoleTextAttribute(hConsole, 11);
								break;
								
							case 'R':
								SetConsoleTextAttribute(hConsole, 12);
								break;
								
							case 'M':
								SetConsoleTextAttribute(hConsole, 13);
								break;
								
							case 'Y':
								SetConsoleTextAttribute(hConsole, 14);
								break;
								
							case 'W':
								SetConsoleTextAttribute(hConsole, 15);
								break;
						}
						
						x += 3;	
					}
					else if (argv[i][x] == '/' && argv[i][x + 1] == '$')
					{
						SetConsoleTextAttribute(hConsole, 7);
						x += 2;	
					}
					
					if (argv[i][x])
						printf("%c", argv[i][x]);
						
					if (!GetKeyState(VK_SPACE))
						Sleep(atoi(argv[1]));
				}
				
				if (argv[i + 1])
					printf(" ");
		}
		
		
		if (argToStart == 3)
			printf("\n");
		
		}
		else
		{
			printf("Creates simulated (colored) typing text with a millisecond delay.\n\n");
			printf("%s [delay] [-n] [text]\n\n", argv[0]);
			printf("-n - optional parameter (adds a line break to the end of the line)\n");
			printf("delay - the delay between each character (in milliseconds)\n");
			printf("text - a string of characters (quotes recommended)\n\n");
			printf("This version of the command offers color support. To add color to pieces of your text, use the string \"/*#\" (without quotes), where # equals one of the corrosponding color codes below. Placing a \"/$\" (without quotes) anywhere in the string will terminate the color and return to standard light gray on black.\n\n");
			printf("Color codes:\n");
			printf("z - Black			b - Dark Blue\n");
			printf("g - Dark Green	   c - Dark Cyan\n");
			printf("r - Dark Red		 m - Dark Magneta\n");
			printf("y - Dark Yellow	  d - Dark Gray\n");
			printf("D - Light Gray	   B - Light Blue\n");
			printf("G - Light Green	  C - Light Cyan\n");
			printf("R - Light Red		M - Light Magneta\n");
			printf("Y - Light Yellow	 W - White\n\n");
			printf("Example: %s 100 -n \"The Following Text Is /*GGreen/$.\"\n\n", argv[0]);
			printf("Programmed by Jake Chappell\n");
			printf("http://www.masterjakeonline.com/ | skateparkceasercash@yahoo.com\n");
		}
}


Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: C - Flushing The Input Buffer

Posted 23 June 2009 - 04:54 AM

View PostMaster Jake, on 23 Jun, 2009 - 03:32 AM, said:

I'm not exactly sure if this is the correct terminology, but does anyone know how to simply flush the input buffer.


I take it you mean standard input.
If I remember correctly :-

#include <stdio.h>

  fflush(stdin);

Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: C - Flushing The Input Buffer

Posted 23 June 2009 - 05:22 AM

As soon as I read the topic title I knew that someone was going to suggest fflush(stdin); however you really should not do this -- or at the very least realize that this is non-standard and only works because your particular compiler/library vendor allows it.

You can not fflush an input stream! The operation is undefined and really does not even make sense. To flush a stream means to ensure that its contents are all written to its destination -- but since an input stream's destination is your program all fflush SHOULD do is tell you to get busy processing data.

Anyway, Microsoft's compiler allows it, but it is undefined by the standard.

Just a few references:

here
here
here


Google will find you many others but what it really boils down to is that standard only defines fflush's functionality on OUTPUT streams.
Was This Post Helpful? 0
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: C - Flushing The Input Buffer

Posted 23 June 2009 - 06:47 AM

View PostNickDMax, on 23 Jun, 2009 - 01:22 PM, said:

As soon as I read the topic title I knew that someone was going to suggest fflush(stdin); however you really should not do this -- or at the very least realize that this is non-standard and only works because your particular compiler/library vendor allows it.


Ah yes. Well done I was wondering who would be first to spot my deliberate mistake.

(Only kidding).
You are perfectly correct, you have caught me, and I apologize most humbly.
Having read the small print, I see that it is library dependent.

:stupid:

Edit : Can't spell either.

This post has been edited by #define: 23 June 2009 - 06:54 AM

Was This Post Helpful? 0
  • +
  • -

#8 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: C - Flushing The Input Buffer

Posted 23 June 2009 - 07:11 AM

Quote

You can not fflush an input stream! The operation is undefined and really does not even make sense. To flush a stream means to ensure that its contents are all written to its destination -- but since an input stream's destination is your program all fflush SHOULD do is tell you to get busy processing data.

Totally agree. But for some strange reason flushing the input buffer actually works on Turbo C++ compilers.
Was This Post Helpful? 0
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: C - Flushing The Input Buffer

Posted 23 June 2009 - 12:00 PM

Quote

But for some strange reason flushing the input buffer actually works on Turbo C++ compilers.

It works on most windows compilers. I actually think this is because windows offers a low level feature for this though I am not 100% sure on that (just remember reading that somewhere).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1