initialize a string variable with output of a command?

G

Glen

Hi,

I just have a quick question
Im trying to initialize a string (or char) variable with the information
created by the output of a shell (or DOS) command..

Example:
I want to execute a command such as
system("cat /test.txt | grep something ") or whatever DOS or linux command
and assign the output of that command to the variable str;

so I tried
char str[128]="system(\"my_command\")";

then when I do, cout<<str;
I get system(\"my_command\"),
which is the acctual command, and not its output

Is there any way I can do it?

Thank you
 
K

Kevin Goodsell

Glen said:
Hi,

I just have a quick question
Im trying to initialize a string (or char) variable with the information
created by the output of a shell (or DOS) command..

There is no standard way. Try a group that discusses your particular
platform & C++ implementation.

-Kevin
 
J

Jerry Coffin

Hi,

I just have a quick question
Im trying to initialize a string (or char) variable with the information
created by the output of a shell (or DOS) command..

[ ... ]
Is there any way I can do it?

None is included in the C++ standard, but if you ask about popen in a NG
specific to the OS you're using, there's a pretty fair chance somebody
will be able to help you.
 
D

David Rubin

Glen said:
Hi,

I just have a quick question
Im trying to initialize a string (or char) variable with the information
created by the output of a shell (or DOS) command..

Example:
I want to execute a command such as
system("cat /test.txt | grep something ") or whatever DOS or linux command
and assign the output of that command to the variable str;

so I tried
char str[128]="system(\"my_command\")";

then when I do, cout<<str;
I get system(\"my_command\"),
which is the acctual command, and not its output

Is there any way I can do it?

Try

system("my_command > command.out");
string s;
ifstream ifs("command.out");
std::getline(ifs, s);
ifs.close();

/david
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top