clearing the buffer??

This is a discussion on clearing the buffer?? within the C Programming forums, part of the General Programming Boards category; Not sure if that it what needs to happen, but heres what is happening and needs to not happen... hah. ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103

    clearing the buffer??

    Not sure if that it what needs to happen, but heres what is happening and needs to not happen... hah. OK so I am using fgets() to capture spaces as well as text. But I am guessing the buffer isn't clear so instead of letting me type and it take from the stdin. There was something there and it auto chose it. So I guess what I am asking is how do I clear the buffer. If you want to see the code just ask, or if you have questions just ask, I'm happy to answer them.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    A forum search will tell you how to do it, but I would suggest you find out what's actually in the stream, what put it there, and correct the source of the problem rather than the symptom.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    ok ya I know what is in there and it is in the same program so I think it is just to be there, but I just need to get it out.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    Well, I found fflush() but it didn't actually work, here is the code with the problem, but before what happens is, when I am "getting" from stdin it automatically puts in ".app" what is going on?!?!

    Code:
    char empty1[25];
    			char empty2[55];
    			char applic[15];
    			char applic2[18];
    	
    			printf("Type in the name of the app\n");
    			fflush(stdin);
    			fgets(applic, 15, stdin);
    			
    			sprintf(applic2, "\"%s\"", applic);
    	
    			sprintf(empty1, "open -a %s", applic2);
    	
    			sprintf(empty2, "%s.app", empty1);
    	
    			system(empty2);
    		}

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Probably because fflush(stdin) is considered to exhibit undefined behavior by all of the C and C++ standards.

  6. #6
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    well do you know how to get the .app out of there? or if it will work? basically the whole issue is because I want to be able to enter something with a space which is the reason for fgets()..

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    22
    First, did you take out fflush(stdin)?

    Second, what exactly did you type in (including tabs, returns, etc) and what exactly did you get in the buffer (if you print it out)?

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Code:
    sprintf(empty2, "%s.app", empty1);
    *cough*

  9. #9
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    OK, now I think there might be a code error.. here is the output of the code...and then the code itself.. I am not sure what is going on.. it seems to me it should work.

    ok heres the output to the console.. btw this is unix.


    new-host:C programs Alex$ ./a.out
    This program can open applications or delete files
    Please select what you like to do
    enter 'Delete' to delete files, or 'open' to open
    open
    Type in the name of the app
    Unable to find application named '
    .app'
    new-host:C programs Alex$

    ok and code

    Code:
    if(toupper(choice[0]) == 'O')
    		{
    	
    	
    			char empty1[25];
    			char empty2[55];
    			char applic[15];
    			char applic2[18];
    	
    			printf("Type in the name of the app\n");
    			fflush(stdin);
    			fgets(applic, 15, stdin);
    			
    			sprintf(applic2, "\"%s\"", applic);
    	
    			sprintf(empty1, "open -a %s", applic2);
    	
    			sprintf(empty2, "%s.app", empty1);
    	
    			system(empty2);
    		}
    PLEASE HELP :0

    oh and btw w/o the fflush(stdin); it does the same thing
    Last edited by alexnb185; 11-23-2007 at 12:37 PM. Reason: adding

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    So instead of taking out the bad fflush() statement, you put another one in? Brilliant.

  11. #11
    Registered User
    Join Date
    May 2005
    Posts
    22
    The .app is there because you're using open to try and open a Mac OS X program. You still need to remove the newline which fgets() reads at the end of your input.

    You still don't want fflush(stdin). Undefined behavior is bad.

  12. #12
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    settle down Mac. I had it copied and I pasted it twice, realized I did so, and then edited it..

  13. #13
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    well I know that, but I want to get it out. Like the only reason for doing any of this because a program like "Photo Booth" has a space and I need the space.. but when I get to the part where fgets is supposed to wait for my input it auto inputs .app and just goes right along.. I don't quite understand what you are saying read the link too.

  14. #14
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    call this function instead of fflush(stdin)

    Code:
    void clear_buffer(void)
    {
         int ch;
         
         while((ch = getchar()) != '\n' && ch != EOF);
    }
    If you would have searched, i might have written this code around 1000 times lol

    ssharish

  15. #15
    Registered User
    Join Date
    May 2005
    Posts
    22
    The .app in the output is normal. It's from running open without a valid program name. When you finish typing your input you press return, which signifies a newline in the terminal. fgets() reads this newline into the string right along with everything else you typed in. Note the comment in the code in the FAQ article I linked to "Now test for and remove..."

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proper method of clearing buffer
    By Oldman47 in forum C++ Programming
    Replies: 14
    Last Post: 04-23-2007, 08:14 PM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Clearing input buffer after using getch()
    By milkydoo in forum C++ Programming
    Replies: 3
    Last Post: 07-22-2003, 12:04 AM
  4. text input buffer clearing
    By red_Marvin in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2003, 03:17 PM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21