how to execute command and get it's result?

S

sonet

in perl
we can execute command and get result(command line stdout) by

$a=`cmdline parameter`;
print $a;

in c
system('cmdline parameter');
but i can not get the result (command line stdout).

in c++?
=====================================================
have any standard method do this job?
 
I

Ian Collins

sonet said:
in perl
we can execute command and get result(command line stdout) by

$a=`cmdline parameter`;
print $a;

in c
system('cmdline parameter');
but i can not get the result (command line stdout).
Direct the output to a file and read the file.
 
S

sonet

Yes!
I can use system systemcall and direct the output to a file
and read the file. May be it is like.

system("cmdline parameter > file");

But if the program is cgi,we need generate a random filename.
It may be a solution,but the method look very stupid.
Have any other smart method to read the stdout from other command line?
 
I

Ian Collins

sonet wrote:

Please don't top-post.
Yes!
I can use system systemcall and direct the output to a file
and read the file. May be it is like.

system("cmdline parameter > file");

But if the program is cgi,we need generate a random filename.
It may be a solution,but the method look very stupid.
Have any other smart method to read the stdout from other command
line?
There will be a better way, but it will be platform specific, so try the
question on a group dedicated to your OS.
 
T

!truth

sonet wrote:

Please don't top-post.









There will be a better way, but it will be platform specific, so try the
question on a group dedicated to your OS.

maybe you can use 'popen'
 
S

SM Ryan

# in perl
# we can execute command and get result(command line stdout) by
#
# $a=`cmdline parameter`;
# print $a;
#
# in c
# system('cmdline parameter');
# but i can not get the result (command line stdout).
#
# in c++?
# =====================================================
# have any standard method do this job?

Since the C standard doesn't even interpret the string in the
system() call, you're out of luck with the C standard.

Other standards provide additional methods, such as the
popen in unix and some other systems. Unix also provides
additional ways to redirect stdin, stdout, and stderr
separately.

Decide what systems you're willing to abandon and code
to that standard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top