C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-02-2007, 06:35 AM   #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.
Afro is offline   Reply With Quote
Old 07-02-2007, 06:48 AM   #2
Eager young mind
 
Join Date: Jun 2006
Posts: 338
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
kris.c is offline   Reply With Quote
Old 07-02-2007, 07:16 AM   #3
Registered User
 
hk_mp5kpdw's Avatar
 
Join Date: Jan 2002
Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,787
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);
__________________
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
--Charles Babbage, 1792-1871

09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0
hk_mp5kpdw is offline   Reply With Quote
Old 07-02-2007, 07:18 AM   #4
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,139
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, "%s", firstprogrampath);
	system(command);
	return 0;
}
edit: dang too late :@
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim
zacs7 is offline   Reply With Quote
Old 07-02-2007, 08:18 AM   #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 %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.
Afro is offline   Reply With Quote
Old 07-02-2007, 08:45 AM   #6
Registered User
 
hk_mp5kpdw's Avatar
 
Join Date: Jan 2002
Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,787
Running a program from within another program is one of the topics in the FAQ.
__________________
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
--Charles Babbage, 1792-1871

09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0
hk_mp5kpdw is offline   Reply With Quote
Old 07-02-2007, 10:37 AM   #7
Registered User
 
Join Date: Jun 2007
Posts: 12
Thanks guys, i got that part to work now.
Afro is offline   Reply With Quote
Old 07-02-2007, 12:50 PM   #8
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,629
__________________
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, etc.

New project: nort
dwks is offline   Reply With Quote
Old 07-03-2007, 12:27 PM   #9
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
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.
brewbuck is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[question]Simulating the Reliability of a Component-Based System [with my code] burbose C Programming 4 06-13-2005 09:03 AM
[question]Simulating the Reliability of a Component-Based System burbose C Programming 3 06-13-2005 07:28 AM
Why Can't C++ Be Used to Develop Operating System? Antigloss C++ Programming 7 05-27-2005 06:16 AM
Putting variables in SYSTEM mycro C++ Programming 3 05-29-2003 07:59 PM
System Calls && Variables Okiesmokie C++ Programming 6 03-06-2002 09:10 PM


All times are GMT -6. The time now is 10:28 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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