Thread: Using variables in system()

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    12

    Unhappy Using variables in system()

    Is there some way to use variables in a system() command?

    i've tried

    Code:
    system("%s",firstprogrampath);
    but i only get an errormessage in the stdlib.h header (line 356), too many arguments in 'int system (const char*)'

    as well in the actual program

    (line 107) at this point in file.

    So, is there any way to use variables in system();?

    edit: also, firstprogrampath[50]; is a char variable. (string variable, i think the real name is?)
    Last edited by Afro; 07-02-2007 at 06:37 AM.

  2. #2
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    Yes, it is possible to do so. Just pay attention to what the man pages say about the inputs that you can pass to the system command. For example, this code will display the contents of the file "test.c"

    Code:
    # include<stdio.h>
    # include<stdlib.h>
    
    int main()
    {
     int num;
     const char abc[]="cat test.c";
     system(abc);
     return 0;
    }
    But, keep in mind that system() does make your code less portable
    In the middle of difficulty, lies opportunity

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    If you must use system...

    Use sprintf to create the command string.

    Code:
    char buffer[100];
    char *firstprogrampath = "foo.exe";
    int value = 10;
    
    ...
    
    /* buffer will contain "foo.exe 10" */
    sprintf(buffer,"%s %d",firstprogrampath, value);
    
    /* Now call system */
    system(buffer);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I think he means something along the lines of:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	char command[BUFSIZ];
    	char firstProgramPath[] = "test";
    
    	sprintf(command, "&#37;s", firstprogrampath);
    	system(command);
    	return 0;
    }
    edit: dang too late :@

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    Seeing as i got a few "If you have to use system..." then is there any other ways to execute a program from a variable?

    The program is supposed to have a menu, with the options to add a program to the list, run a program from the list, remove a program from the list and save the list to the harddrive, all functions are yet to be implemented.

    EDIT, also, here is the code (i am quite the newcomer so it is quite bulky, but it does what it should do atm).

    Any tips or tricks on what could be improved are welcome.

    Code:
    /* A program written by Afro for users to add
    and start various applications from a list */
    
    
    /*  
    
    TODO list 
    
    -Switch-case main menu and sub menu if possible
    -"Run the program" function 
    -"delete latest program" function
    -"exit" function -- DONE
    -"save data" function
    -Replace long IF-firstprogramexist with a function if possible
    -Improve layout and add screen clearing
    */
    
    
    /*------------------------------------------------------------------*/
    /*----------------------- Headers ----------------------------------*/
    /*------------------------------------------------------------------*/
    
    #include <stdio.h>
    #include <stdlib.h>
    
    /*------------------------------------------------------------------*/
    /* ---------------------- Functions --------------------------------*/
    /*------------------------------------------------------------------*/
    
    
    /*------------------------------------------------------------------*/
    /* -------------------- Menu Variables -----------------------------*/
    /*------------------------------------------------------------------*/
    
    int firstChoice;
    int secondChoice;
    int programnumber;
    
    /*------------------------------------------------------------------*/
    /*------------------- Program variables ----------------------------*/
    /*------------------------------------------------------------------*/
    
    /* First program */
    char firstprogramname[30];
    char firstprogrampath[50];
    int firstprogramexist;
    
    /* Second program */
    char secondprogramname[30];
    char secondprogrampath[50];
    int secondprogramexist;
    
    /*Third Program */
    char thirdprogramname[30];
    char thirdprogrampath[50];
    int thirdprogramexist;
    
    /*Fourth Program */
    char fourthprogramname[30];
    char fourthprogrampath[50];
    int fourthprogramexist;
    
    /*Fifth Program */
    char fifthprogramname[30];
    char fifthprogrampath[50];
    int fifthprogramexist;
    
    /*------------------------------------------------------------------*/
    /* ---------------- End of the beginning ---------------------------*/
    /*------------------------------------------------------------------*/
    
    int main()
    {
    programnumber=1;
    
    /* Print info about the program */
    printf("This is a program-starter written by Afro\n");
    printf("It has a maximum capacity of five stored programs\n");
    
    /*------------------------------------------------------------------*/
    /*----------------------- MAIN MENU --------------------------------*/
    /*------------------------------------------------------------------*/
    
    for(;;)
    {
            printf("\nWhat do you want to do?\n\n");
            printf("Run a program (Enter 1)\n");
            printf("Show the programs in the list (Enter 2)\n");
            printf("Add a program to the list (enter 3)\n");
            printf("Delete the latest added program (enter 4)\n");
            printf("Exit (enter 5)\n\n");
            firstChoice=getchar();
            fflush(stdin);
            
    /*------------------------------------------------------------------*/        
    /*---------------------- Execute the programs ----------------------*/
    /*------------------------------------------------------------------*/
    
        if (firstChoice==49)
        {
             printf("Enter the corresponding number \n");
             printf("of the program you want to run.\n");
             fflush(stdin);
             secondChoice=getchar();
             
             if(secondChoice==49)
             {
                  printf("Running &#37;s",firstprogramname);
                  //system("%s",firstprogrampath);
             }
             else if(secondChoice==50)
                  {
                  printf("Running %s",secondprogramname);
                  //system("");
                  }
             else if(secondChoice==51)
                  {
                  printf("Running %s",thirdprogramname);
                  //system("");
                  }
             else if(secondChoice==52)
                  {
                  printf("Running %s",fourthprogramname);
                  //system("");
                  }
             else if(secondChoice==53)
                  {
                  printf("Running %s",fifthprogramname);
                  //system("");
                  }
             else
             {
                     printf("Please enter a correct number between 1-5.");
             }
        
    }    //end of the run the program "if"    
    /*------------------------------------------------------------------*/        
    /*---------------------- List the programs -------------------------*/
    /*------------------------------------------------------------------*/
    
        if (firstChoice==50)
        {
             printf("You wish to run a program\n");
             printf("These are the programs that the computer has stored:\n");
        
         
        if(firstprogramexist==1)
        {
                                  printf("Number 1:\t %s \n",firstprogramname);
        }
        
        if(secondprogramexist==1)
        {
                                  printf("Number 2:\t %s \n",secondprogramname);
        }
        
        if(thirdprogramexist==1)
        {
                                  printf("Number 3:\t %s \n",thirdprogramname);
        }
        
        if(fourthprogramexist==1)
        {
                                  printf("Number 4:\t %s \n",fourthprogramname);
        }
        
        if(fifthprogramexist==1)
        {
                                  printf("Number 5:\t %s \n",fifthprogramname);
        }
        
        else printf("There are no programs in the list.\n");
    } //end of the list the programs "IF"
    
    /*------------------------------------------------------------------*/    
    /*------------- The adding of the five possible programs -----------*/
    /*------------------------------------------------------------------*/    
    
        else if (firstChoice==51 && programnumber==1)
        {
             printf("You wish to add a program\n");
             printf("Please enter the name of the program:\n\n");
             gets(firstprogramname);
             printf("\n\nPlease enter the path to the program:\n");
             fflush(stdin);
             gets(firstprogrampath);
             printf("The program will be shown as:\n %s\n\n",firstprogramname);
             printf("The path to the program is:\n %s\n\n",firstprogrampath);
             programnumber++;
             firstprogramexist=1;
        }
        
        else if (firstChoice==51 && programnumber==2)
        {
             printf("You wish to add a program\n");
             printf("Please enter the name of the program:\n\n");
             gets(secondprogramname);
             printf("\n\nPlease enter the path to the program:\n");
             fflush(stdin);
             gets(secondprogrampath);
             printf("The program will be shown as:\n %s\n\n",secondprogramname);
             printf("The path to the program is:\n %s\n\n",secondprogrampath);
             programnumber++;
             secondprogramexist=1;
        }
        
        else if (firstChoice==51 && programnumber==3)
        {
             printf("You wish to add a program\n");
             printf("Please enter the name of the program:\n\n");
             gets(thirdprogramname);
             printf("\n\nPlease enter the path to the program:\n");
             fflush(stdin);
             gets(thirdprogrampath);
             printf("The program will be shown as:\n %s\n\n",thirdprogramname);
             printf("The path to the program is:\n %s\n\n",thirdprogrampath);
             programnumber++;
             thirdprogramexist=1;
        }
        
        else if (firstChoice==51 && programnumber==4)
        {
             printf("You wish to add a program\n");
             printf("Please enter the name of the program:\n\n");
             gets(fourthprogramname);
             printf("\n\nPlease enter the path to the program:\n");
             fflush(stdin);
             gets(fourthprogrampath);
             printf("The program will be shown as:\n %s\n\n",fourthprogramname);
             printf("The path to the program is:\n %s\n\n",fourthprogrampath);
             programnumber++;
             fourthprogramexist=1;
        }
        
        else if (firstChoice==51 && programnumber==5)
        {
             printf("You wish to add a program\n");
             printf("Please enter the name of the program:\n\n");
             gets(fifthprogramname);
             printf("\n\nPlease enter the path to the program:\n");
             fflush(stdin);
             gets(fifthprogrampath);
             printf("The program will be shown as:\n %s\n\n",fifthprogramname);
             printf("The path to the program is:\n %s\n\n",fifthprogrampath);
             programnumber++;
             fifthprogramexist=1;
        }
    
    /* Program-adding error (too many programs)*/
        else if (firstChoice==51 && programnumber>5)
        {
             printf("You have reached the maximum amount of programs");
        }
    
    /*------------------------------------------------------------------*/    
    /*----------------- END OF PROGRAM ADDING --------------------------*/
    /*------------------------------------------------------------------*/    
    
    
    /*------------------------------------------------------------------*/    
    /*----------------- DELETE LATEST ADDED PROGRAM --------------------*/
    /*------------------------------------------------------------------*/    
    
    
    /*------------------------------------------------------------------*/    
    /*--------------------------- EXIT ---------------------------------*/
    /*------------------------------------------------------------------*/    
    
         else if (firstChoice==53)
         {
                             break;
         }
    
    /*False Character error */    
        else 
        {
             printf("Enter a correct number.\n");
        }
    
    
    
    
    }         // End of Loop
    
    return(0);
    }         // End of program
    Last edited by Afro; 07-02-2007 at 08:28 AM.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Running a program from within another program is one of the topics in the FAQ.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    Thanks guys, i got that part to work now.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Afro View Post
    Seeing as i got a few "If you have to use system..." then is there any other ways to execute a program from a variable?
    Since you are designing a menu system, I see no reason why system() is not appropriate. Go ahead and use it. The other answers about using sprintf() to create the command string are the right info.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-13-2005, 09:03 AM
  2. Replies: 3
    Last Post: 06-13-2005, 07:28 AM
  3. Why Can't C++ Be Used to Develop Operating System?
    By Antigloss in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2005, 06:16 AM
  4. Putting variables in SYSTEM
    By mycro in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2003, 07:59 PM
  5. System Calls && Variables
    By Okiesmokie in forum C++ Programming
    Replies: 6
    Last Post: 03-06-2002, 09:10 PM