Hello,
I want to run exe file during running a program and there is some input for this file how can I do it in C++/C?Is it possible or no I should use another language.:rolleyes:
6 replies to this topic
#1
Posted 11 January 2012 - 12:41 AM
|
|
|
#2
Posted 11 January 2012 - 02:05 AM
Read about system() function in cstdlib / stdlib.h
#3
Posted 11 January 2012 - 03:30 AM
system just open file but it doesn't help me to pass parameter.
#4
Posted 11 January 2012 - 06:22 AM
Sure it does. Look at the header:
int system ( const char * command );You can build your command (program + arguments).
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#5
Posted 11 January 2012 - 07:26 AM
Can you give me and example
for example I want to run this program (What are my arguments)
system("start test.exe");
to run test.exe
for example I want to run this program (What are my arguments)
//test.exe
#include <iostream>
using namespace std;
int main()
{
int a,b;
cin>>a;
cout<<a;
cin>>b;
cout<<b;
return 0;
}
I use system("start test.exe");
to run test.exe
#6
Posted 11 January 2012 - 08:47 AM
If you're calling this program from another one, then yes (I think you don't have to type start, not sure). If you want to pass arguments to this program (test.exe) then you need to change main function header to:
Calling test.exe from another program with arguments:
int main(int argc, char* argv[])argc is argument count and argv is a argument vector (cstrings). Note that argv[0] is program's name, so you will always have at least 1 argument.
Calling test.exe from another program with arguments:
system("test.exe 1 2");
A conclusion is where you got tired of thinking.
#define class struct // All is public.
#7
Posted 11 January 2012 - 10:23 AM
Assuming you have a POSIX compliant system (not windows), i would suggest using exec, execvp, execle class of functions along with fork().
This is the standard way of calling an executable from with in a program. Basically exec is passed an executable path + name and parameters and it replaces the current program's image in memory with the new one (whose path + name and parameters are passed).
Since we do not want the caller program to be wiped off, so first we create a child process with fork, and inside the child we call exec.
There is plenty of details about exec class of functions on google.
The reason i preferred them over system is that system basically works in accordance with a terminal or shell. Conclusively, any thing that you pass to system command is like executing those on a shell. Your program might not have a terminal access or it might not be as fast compared to exec.
Fayyaz
This is the standard way of calling an executable from with in a program. Basically exec is passed an executable path + name and parameters and it replaces the current program's image in memory with the new one (whose path + name and parameters are passed).
Since we do not want the caller program to be wiped off, so first we create a child process with fork, and inside the child we call exec.
There is plenty of details about exec class of functions on google.
The reason i preferred them over system is that system basically works in accordance with a terminal or shell. Conclusively, any thing that you pass to system command is like executing those on a shell. Your program might not have a terminal access or it might not be as fast compared to exec.
Fayyaz
Today is the first day of the rest of my life
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









