tags:

views:

870

answers:

6

Hello,

I run some commands with the C++ system() function (int system ( const char * command );). How can I collect the return value of the issued commands?

EDIT: To be more specific: I want to collect the output of the issued command. i.e. the directory listing of the dir command.

Thanks,
mspoerr

+7  A: 

system() returns an int, so just grab it: int rvalue = system(command);

I believe the exact details of what system() will return are system-specific, though.

phoebus
They are indeed : 7.20.4.6 The system function :If the argument is a null pointer, the system function returns nonzero only if acommand processor is available. If the argument is not a null pointer, and the systemfunction does return, it returns an implementation-defined value
Samuel_xL
I don't need the retrun value of system() - I want to collect the return string of the issued command...
mspoerr
What do you think system returns? It returns the return value of the command.
GMan
I know, that system() returns an "int". Therfore I asked if there is a way to collect the return value of the issued command.
mspoerr
The value of that int IS the return value of the command. Perhaps you mean you want to collect the output to one of the streams like stdout?
phoebus
@mspoerr: I think you need to explain "return value of the issued command", since you seem to use "return value" in an unconventional way (if it is *not* the int that you are after). If you really don't know how to store an int in a variable, we have an even deeper problem communicating.
Martin v. Löwis
+1  A: 

system() is declared and defined in libc. You can either read the first link I provided, or do man system at a command prompt in your shell.

Jonathan Feinberg
+3  A: 

There are typically two ways for a system program to "return" a value: by writing to stdout, and by returning a status integer at the end of the program. (there are often more ways to return results, eg. by writing to a file or into a database, but I assume those are out of scope here).

For receiving the status code, just check the return value of the system function.

For receiving the output, either redirect it into a file, and read the file afterwards, or use popen.

Martin v. Löwis
+11  A: 

Are you looking for returned value (as in "exit status") of the executed command, or for its output (as in "what did it print")?

If the latter, use popen(3) instead.

Employed Russian
Thank you for the hint - I will try with popen()
mspoerr
Isn't system just a passthru call in the sense that stdout/stderr are passed through to the paren't stdout/stderr?
David
@David: yes, but the OP doesn't want it to be passed through. he wants to collect it in a string in his program
newacct
+3  A: 

The return value of system is (ironically) system-dependent, but in POSIX systems (including Linux, etc), it's the same as for wait -- low 8 or 16 bits are the exit status of the child (probably what you mean by "value returned by"), higher bits indicating what kind of signal terminated the child, if any. The URL to the manpage I've given supplies the preprocessor macros you can use to pry apart that return value!

There is no such thing as a "return string" of a program, as you've now clarified in a comment is what you desire; as another answer already mentioned, if you want the text which gets output by the other program, you should use popen instead of system.

Alex Martelli
A: 

I suggest the popen() functions, as said by other people as well, but this problem is platform specific. the popen() function is available on operating systems that use the POSIX API. I am not sure if this command would work on other APIs like WIN32

andremo