question about system commands

This is a discussion on question about system commands within the C Programming forums, part of the General Programming Boards category; Ok this is a very simple program I am just making to play around. but I ran into a problem, ...

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

    question about system commands

    Ok this is a very simple program I am just making to play around.
    but I ran into a problem, heres the code:

    Code:
    #include <stdio.h>
    #include <unistd.h>
    
    main()
    {
    
    	char app[6];
    	
    	printf("Type the name of the app you would like to open.\n");
    	scanf(" &#37;s", app);
    	printf(" %s\n", app);
    	
    	system("open -a %s.app", app);
    	
    	return 0;
    }
    if you don't know about the unistd.h header don't worry that is not the problem. What happens is when I try to compile it says that there are too many arguments to function "system." However if I just make a program with everything the same but instead just get rid of the "%s" and put in itunes in the system command, it works... So I guess what I am asking is why is that and how can I fix it or write it correctly?
    Last edited by alexnb185; 11-22-2007 at 11:27 AM.

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    The function 'system()' only takes one string as an argument.

    Look into sprintf.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    from what I read that doesn't help me.. it said that it converts numbers into a string of text? so I'm not saying your wrong but I guess I am asking how I could use it here?

  4. #4
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by alexnb185 View Post
    from what I read that doesn't help me.. it said that it converts numbers into a string of text? so I'm not saying your wrong but I guess I am asking how I could use it here?
    The 'sprintf()' function allows you to use format specifiers from the printf family(&#37;s, %d, and so on) but instead of printing it to the screen, print it to an array. This benefits you, because:

    Code:
    // ..
    
    char name_of_file[] = "lolomg.txt", empty_array[50];
    
    sprintf(empty_array, "notepad %s", name_of_file);
    
    puts(empty_array);
    
    // ...
    If you execute this bit of code, you'll see how it helps you.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    I am just using gcc to compile under leopard

  6. #6
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by Adak View Post
    In your program, scanf() is going to need the address of operator '&', added to it, so app is set up properly.

    System() is passed a literal, it's not like printf(), and won't substitute the value of app for %s, (even if app is set up properly).

    Does your compiler support a system() function that can be passed something else, besides a string literal?
    An array does not need the address operator before it when passed to scanf or other input functions. Passing an array like that passes a pointer to the first element of it, eg, the address of the array. So, long story short, no need for it.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I believe IceDane's post about using sprintf(), is your answer. Play around with his code and see if you don't agree.

  8. #8
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    ok, I compiled that code Icedane. What happened was it printed "notepad lolomg.txt" But I don't see how I can use that if the system() function won't let you put in strings.. or is it possible to do this

    Code:
    .....
    system(empty_array);
    ......
    and it would send "notepad lolomg.txt" to the sytem?

  9. #9
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    and the answer is yes.. it did do that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File System Question
    By Phoenix_Rebirth in forum C Programming
    Replies: 5
    Last Post: 01-23-2008, 11:54 AM
  2. System commands
    By Tropicalia in forum C++ Programming
    Replies: 3
    Last Post: 09-30-2006, 11:18 AM
  3. Running linux system commands
    By Creini in forum C Programming
    Replies: 2
    Last Post: 05-27-2006, 06:56 AM
  4. running system commands??
    By killdragon in forum C++ Programming
    Replies: 13
    Last Post: 09-25-2004, 02:49 PM
  5. what can i type into msdn to bring up all the system commands
    By Shadow12345 in forum Windows Programming
    Replies: 10
    Last Post: 12-30-2002, 05:58 PM

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