Thread: clearing the buffer??

  1. #16
    Registered User
    Join Date
    May 2005
    Posts
    22
    Quote Originally Posted by ssharish2005 View Post
    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
    How's that needed in alexnb185's code? Wouldn't that just make the user press return or ctrl-D (or if they have a more interesting way of sending EOF...) before typing the name of the app? Reading into a large buffer with fgets() should virtually avoid the "flushing the buffer" problem. The buffer doesn't seem to be a problem in this case. The problem is the newline being read in by fgets(), which the couple of lines below the comment in the article I specified should take care of.

  2. #17
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well OK if that was the case use a strchr to get elimiated witht he '\n' char. And perhaps the reason why i posted that code was that i saw a fflush(stdin) call on the code. And hence i posted the code.

    And if the '\n' was the main thing bothering you try replaced them with the '\0' char.

    ssharish

  3. #18
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    well no I can't type anything at all.. it just puts in .app for me.. I am not sure if you understand that.. maybe you do.. maybe I am just a moron and don't understand you.. idk...

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > printf("Type in the name of the app\n");
    > fflush(stdin);
    > fgets(applic, 15, stdin);
    > sprintf(applic2, "\"%s\"", applic);
    1. Remove ALL your fflush(stdin), it's undefined, and on any unix system it does nothing (at best) on an input stream.
    2. Use fgets() with a large temporary buffer, not a short final result.
    3. In this instance, you'll be wanting to remove the newline from the end of the line, which fgets stores.

    Code:
    char buff[BUFSIZ];
    printf("Type in the name of the app\n");
    fgets(buff, sizeof buff, stdin);
    
    { char *p = strchr(buff,'\n'); if ( p ) *p = '\0'; } /* delete the newline, if present */
    
    strncpy( applic, buff, sizeof applic );              /* safe copy of user input */
    applic[sizeof applic - 1] = '\0';                    /* make sure it's a valid string */
    
    sprintf(applic2, "\"%s\"", applic);
    But you could do the whole thing in one call, say
    Code:
    sprintf( empty2, "open -a \"%s\".app", applic );
    But shouldn't the .app be inside the double quotes?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #20
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    Quote Originally Posted by alexnb185 View Post
    well no I can't type anything at all.. it just puts in .app for me.. I am not sure if you understand that.. maybe you do.. maybe I am just a moron and don't understand you.. idk...
    It's very understandable. Here's what they have been trying to tell you.

    The problem is not that the newline is left in stdin at all. fgets() reads the user's string, AND the newline sequence and places both in your buffer. (applic).

    With subsequent sprintfs(), you are carrying the newline sequence to subsequent buffers. Finally, when you issue the "open" command to system(), you are passing the newline sequence in the middle of your command, like this:

    open -a "Photo Booth\r\n".app

    To fix this, when you read the user's application name AND their newline, you need to remove the newline sequence from the end of the string. An easy way to do this is with strtok():

    Code:
    	printf("Type in the name of the app\n");
    	fgets(applic, 15, stdin);  // read user's string, including the newline 
    	sprintf(applic2, "\"%s\"", strtok(applic, "\r\n") );  // truncate user's string just before the newline sequence
    	sprintf(empty1, "open -a %s", applic2);
    	sprintf(empty2, "%s.app", empty1);
    Using both the carriage return (\r) and line feed (\n) as tokens, you will grab whichever one comes first in the string. (On a Mac, the \r).

    Todd
    Last edited by Dino; 11-24-2007 at 10:15 AM. Reason: typo

  6. #21
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    Todd! thank you so much! That makes perfect sense now! Really, thank you.

  7. #22
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    Well, that still didn't fix it, it happened again, only it put a (Null) before the usual app

    heres the output in the console:

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

  8. #23
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    Don't put quotes around the .app, just around the application name. It absolutely works for me.

    Todd

  9. #24
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    I didn't, I have it with the quotes only around the name of the application, or exactly what the user types in... heres the code:

    Code:
    printf("Type in the name of the app\n");
    			
    			
    			fgets(applic, 15, stdin);
    			
    			sprintf(applic2, "\"%s\"", strtok(applic, "\r\n") );
    	
    			sprintf(empty1, "open -a %s", applic2);
    	
    			sprintf(empty2, "%s.app", empty1);
    	
    			system(empty2);

  10. #25
    Registered User
    Join Date
    May 2005
    Posts
    22
    strtok() returns a pointer to a string, not the string itself. I recommend going over Salem's post. If you can use and understand the code in that post, you will have fewer problems with future input (it uses good programming practices, like safely copying strings).

  11. #26
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > sprintf(applic2, "\"%s\"", strtok(applic, "\r\n") );
    1. Unless you open a file in binary mode (unusual for a text file, less so for stdin), then the only termination character in the buffer will be \n.

    2. If the user decides to type in too much data, then there will be NO newline in the input and strtok() will return NULL. Which probably explains where the (null) was coming from in the following test by alexnb185.

    A buffer of 15 chars will only get you 13 chars of useful input, a newline and a \0.
    This is why I suggest a large buffer + some careful validation to being with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #27
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    ok well Salem I tryed what you said in the post right above me, but it still does it :[ the same (NULL).app thing... this is of course without me typing anything at all

  13. #28
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    OK, this is getting to be quite the little project lol... but Salem I did what you said to do in the earlier post as well, it did eliminate the (NULL), however the .app is still there in the console, so, still dealing with that... heres the code:

    Code:
    char empty1[40];
    			char empty2[65];
    			char applic[25];
    			char applic2[28];
    			char buff[BUFSIZ];
    				printf("Type in the name of the app\n");
    				fgets(buff, sizeof buff, stdin);
    
    				{ char *p = strchr(buff,'\n'); if ( p ) *p = '\0'; } /* delete the newline, if present */
    
    				strncpy( applic, buff, sizeof applic ); 			/* safe copy of user input */
    				
    					applic[sizeof applic - 1] = '\0'; 
    					
    			sprintf(applic2, "\"%s\"", applic);
    	
    			sprintf(empty1, "open -a %s", applic2);
    			
    			sprintf(empty2, "%s.app", empty1);
    	
    			system(empty2);

  14. #29
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    So do something like
    printf( "Command=%s\n", empty2 );
    and see what it prints.

    From what I can make out, you end up with
    open -a "user input".app

    My guess is the .app needs to be inside the double quotes along with the rest of your input.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #30
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    Perhaps the problem you are having is that there is already a newline in the buffer prior to this function being called. With this small snippet of code, it works perfectly for me. I entered Finder and Finder was opened.

    Todd

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, 07: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-21-2003, 11:04 PM
  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