Thread: clearing the buffer??

  1. #31
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    well, maybe you have a super computer but for me it doesn't and the whole reason for jumping through these hoops is to get programs with spaces to work. and Salem, this is what I got from doing that....



    This is empty2!!!! open -a "".app
    new-host:C programs Alex$



    i just put this is empty2!!!! in printf() so it was easy to see.. so that is what we are telling the system.. something is messed up somewhere...
    Last edited by alexnb185; 11-24-2007 at 12:38 PM. Reason: making easier to read

  2. #32
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    fgets(buff, sizeof buff, stdin);
    char *p = strchr(buff,'\n'); if ( p ) *p = '\0'; 
    strncpy( applic, buff, sizeof applic );				
    applic[sizeof applic - 1] = '\0';
    I would check the content of buff after reading the values from the user. Print the buff aafter fgets and see what u get printed. And perhaps in this code

    Code:
    strncpy( applic, buff, sizeof buff  );
    it should have been buff. So that u are copying the while content was buff to applic. Make sure applic has sufficint space to hold the data.

    Code:
    applic[sizeof applic - 1] = '\0';
    and u dont need this since strncpy will append null for u.

    ssharish

  3. #33
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    if I print the buff after fgets() I just get a straight line across.. like blank. Doesn't that mean it is clear?

  4. #34
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    OK, I got it to work!!! I don't know why what I changed made it work but heres what did. At the beginning of the whole thing it asks whether you want to open or delete a file, and for what we were trying to fix, I would type in open( all that really matters is the "o") but anyways I was using

    Code:
    scanf(" %s", choice)
    The problem was comming later when I wanted to know the program the user wanted to open. The whole issue started because some programs had spaces and we were trying to make those work also. But, when it asked for the program you wanted to open it just skipped right past the fgets() step. But by changing the scanf() with the choice, to another fgets() statement it worked. I am guessing there was a newline character somewhere in the buffer we couldn't figure out how to get rid of. But it works now, heres the final code.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    
    main()
    {
    	
    	char choice[8];
    	
    	
    	printf("	This program can open applications or delete files\n");
    	printf("	Please select what you like to do\n");
    	printf("	enter 'Delete' to delete files, or 'open' to open\n");
    	fgets(choice, 8, stdin);
    	
    	if((toupper(choice[0]) == 'O')||(toupper(choice[0]) == 'D'))
    	{
    	
    		if(toupper(choice[0]) == 'O')
    		{
    	
    	
    			char empty1[40];
    			char empty2[65];
    			char applic[25];
    			char applic2[28];
    			printf("Type in the name of the app");
    			
    			fgets(applic, 28, stdin);
    				
    			char *p = strchr(applic,'\n'); if ( p ) *p = '\0'; 
    				
    					
    			sprintf(applic2, "\"%s.app\"", applic);
    	
    			sprintf(empty1, "open -a %s", applic2);
    	
    			system(empty1);
    		}
    		if(toupper(choice[0]) == 'D')
    		{
    			
    			char location[45], location2[46], name[23], systemname[28];
    			
    			printf("Type the location of the file\n");
    			printf("Ex: Macintosh HD is in the folder '/Users/Alex/Desktop'\n");
    			printf("Keep in mind capitalization DOES COUNT\n");
    			printf("so now tell me the address of the file\n");
    			
    			scanf(" %s", location);
    			
    			sprintf(location2, "%s/", location);
    			
    			printf("Now tell me the name and type of file it is\n");
    			printf("Ex: examplefile.txt\n");
    			scanf(" %s", name);
    			printf(" %s\n", name);
    			
    			sprintf(systemname, "%s%s", location2, name);
    			
    			printf("%s.....", systemname);
    			
    			remove(systemname);
    			
    			printf("deleted\n");
    			
    			
    			
    		}
    		
    	}
    	else
    	{
    		printf("An error occured please restart\n");
    	}
    	
    	return 0;
    }
    Last edited by alexnb185; 11-24-2007 at 02:20 PM.

  5. #35
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by Todd Burch View Post
    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
    Quote Originally Posted by alexnb185
    well, maybe you have a super computer but for me it doesn't and the whole reason for jumping through these hoops is to get programs with spaces to work. and Salem, this is what I got from doing that....
    Or perhaps I don't have a super computer?

    Todd

  6. #36
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    Well, even if that was the problem which it probably was, how would I fix that without doing what I did?

  7. #37
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    See this FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    You will see that fgets() is the preferred function to get data from a user.

    Todd

  8. #38
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Mixing scanf() and fgets() will lead to problems of fgets() reading an empty string at some point or other.

    IMO, using fgets() to read ALL input into buff, then using sscanf(), strncpy() or any other string function you like to validate and parse the data is the only way to go.
    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.

  9. #39
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    Hey, I want to say thanks to everyone that helped me because this took awhile and I am just kinda getting started with all this and its nice to have some people out there helping me out. So thanks.

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