Thread: Saving spaces in arrays?

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

    Saving spaces in arrays?

    OK, heres whats happening. This part of the program can open a .app which the user specifies. It works perfectly on everything... except when there are spaces. For example (I am on a mac) if I was to open itunes, I would simply type in itunes, but if I want to open Photo Booth, im screwed because of the spaces. Now I already have the program to set quotes, but I think the problem is that I can't scan for spaces. I am using scanf() right now to get the program name, tryed gets() but that didn't work at all, it just skipped right past it. but anyway heres the code. This was kinda hard to explain so if you have a question just ask.

    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");
    			scanf(" %s", applic);                                      //scaned here
    			
    			sprintf(applic2, "\"%s\"", applic);
    	
    			sprintf(empty1, "open -a %s", applic2);
    	
    			sprintf(empty2, "%s.app", empty1);
    	
    			system(empty2);
    		}

  2. #2
    Registered User
    Join Date
    May 2005
    Posts
    22
    Look into using fgets() to read into a large buffer. Much better than scanf().

  3. #3
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    well I am not reading from a file. this is just user keyboard. I am not sure how to do what I am about to say or if it is possible this part of C has always confused me ALOT. but are you talking about taking stdin (is that what it is) and using that?

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    22
    stdin counts as a file. Just put stdin where you'd put a file pointer and it reads the keyboard input.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    so you do something like this...

    Code:
    char string[20];
    
    fgets(string, 20, stdin);
    
    puts(string);
    That would take everything I hit on the keyboard and print it to the console correct?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    fgets works on streams. Stdin is one such stream, to the default input (console window unless you redirect it).
    When opening files via fopen, the files are wrapped in a stream.

  7. #7
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    ok now I did that, and it did what I wanted... ish.. only there was something in the buffer I think its called.. so without me doing anything it entered .app for me.. and so obviously didn't work.. is there a way I can clear all of that before I use fgets()?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  2. saving a CString to binary file with trailing spaces
    By nineofhearts in forum C++ Programming
    Replies: 12
    Last Post: 01-03-2006, 11:53 AM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM
  5. Spaces in Character Arrays
    By ADLOTS in forum C++ Programming
    Replies: 3
    Last Post: 11-25-2002, 04:24 AM