Thread: How to execute other program within c program?

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    6

    Question How to execute other program within c program?

    Dear All,

    How to execute other program within a C program?

    For example,
    I have a one program. This program executes in command prompt as follows. The aguments of SNMP.exe program is "526" and "localhost".

    C:\>SNMP.exe 526 localhost

    I want to execute the "SNMP.exe" program within a C program using system() function or others.

    Please give me simple example.

    Thank you in advance.

    ysko

  2. #2
    Registered User jephthah's Avatar
    Join Date
    May 2010
    Location
    seattle
    Posts
    49
    Quote Originally Posted by KoYoungSuk View Post
    Please give me simple example.

    okay.

    use system() with the program name and argument, as its argument.

    simple, huh?

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    6
    Quote Originally Posted by jephthah View Post
    okay.

    use system() with the program name and argument, as its argument.

    simple, huh?
    Dear jephthah

    Thank you for quick response.

    Please more explain. Can you support the simpe example?

    Thank you in advance.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64
    Here is the example for using system(),

    Code:
    #include<stdio.h>
    main()
    {
            system("test.exe  5434 localhost");
    }
    But it is not the efficient way in C .For efficiency use execlp.
    Last edited by karthigayan; 06-06-2010 at 10:51 PM.

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    6
    Quote Originally Posted by karthigayan View Post
    Here is the example for using system(),

    Code:
    #include<stdio.h>
    main()
    {
            system("test.exe  5434 localhost");
    }
    But it is not the efficient way in C .For efficiency use execlp.
    Dear karthigayan,

    Thank you for quick response.

    If C program have a prameter, How to use the system function?

    main (int argc, char **argv) {



    system ("test.exe %s %s", argv[1], argv[2])

    }

    Is it right?

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by KoYoungSuk View Post
    Dear karthigayan,

    Thank you for quick response.

    If C program have a prameter, How to use the system function?

    main (int argc, char **argv) {



    system ("test.exe %s %s", argv[1], argv[2])

    }

    Is it right?
    If by this you mean how do you call another program whose command line arguments are given as the command line arguments to your program, then yes.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7

  8. #8
    Registered User
    Join Date
    Jun 2010
    Posts
    6
    Quote Originally Posted by Fordy View Post
    Dear jephthah,

    Thank you for quick response.

    contents of snmp.bat file
    ----------------------------------
    snmptrap -v 1 -c public %1 .1.3.6.7.2.1.3.1 %2 1 6 ' .1.3.6.7.2.1.3.1.1.2 s '%3,%4,%5,%6'

    contents of c file.
    ----------------------------------
    execlp("snmptrap.exe","-v","1","-c","public","argv[0]",".1.3.6.7.2.1.3.1","argv[1]","1","6","'",".1.3.6.7.2.1.3.1.1.2","s","'"," argv[2]","argv[3]","argv[4]","argv[5]","'",0);

    Is it possible?

    Thank you in advance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. im a noob at c++, do you think so?
    By belRasho in forum C++ Programming
    Replies: 6
    Last Post: 04-25-2010, 11:02 PM
  2. a program that will call an execute but with arguments
    By what3v3r in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2006, 09:44 PM
  3. Need help on code to execute the program
    By hibmem10 in forum C++ Programming
    Replies: 6
    Last Post: 12-24-2005, 01:42 PM
  4. Can't execute my program
    By bahruddina in forum C Programming
    Replies: 4
    Last Post: 07-13-2004, 10:19 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread