[Dev-C++] Re: execution of a string with system()
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Christophe C. <chr...@fr...> - 2002-09-19 07:32:52
|
Hello,
think Abhijit Shylanath mistyped his response - should be (untested too) :
string whatever = "foo";
system(whatever.c_str());
This will execute the command "foo" on your system.
Little tip : system() returns an integer value, but on many systems, this
is not the plain return code of the executed command. If you need to test
the command's return code, use (will work on many - but maybe not all -
systems) :
int returncode=system(whatever.c_str()) >> 8;
This is because on these systems, the return code of the command is stored
in the 8 upper bits of a 16 bits integer. The ">> 8" says "take all the
bits of this integer and shift them 8 times on right". Thus, the 8 upper
bits takes place of the 8 lower ones, and the value of the whole integer
becames now the command's return code.
Example :
0011001100011100 >> 8 = 0000000000110011
Before ending, a little warning : if you want to execute system commands in
a multi-threaded program, you MUSN'T use the system() function. You must
then use a combination of fork() and for example waitpid() commands.
If you need a sample, query on the list.
|