Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > initialize a string variable with output of a command?

Reply
Thread Tools

initialize a string variable with output of a command?

 
 
Glen
Guest
Posts: n/a
 
      09-27-2003
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



 
Reply With Quote
 
 
 
 
Kevin Goodsell
Guest
Posts: n/a
 
      09-27-2003
Glen wrote:
> 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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

 
Reply With Quote
 
 
 
 
Jerry Coffin
Guest
Posts: n/a
 
      09-27-2003
In article <EGndb.67483$(E-Mail Removed)-kc.rr.com>, http://www.velocityreviews.com/forums/(E-Mail Removed)
says...
> 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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
David Rubin
Guest
Posts: n/a
 
      09-29-2003
Glen wrote:
>
> 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

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
How to initialize variable of type Vector<Term>[] in Java 5.0? Ulrich Scholz Java 5 12-05-2005 05:32 PM
initialize static structured variable challenge wim delvaux C++ 5 06-23-2004 01:23 PM
Simulatneous declare/initialize member variable Fred Ma C++ 10 06-04-2004 07:11 AM
help me rectify the code---why cant i initialize the double variable sum1=0 Shashidhar Patil C++ 3 09-09-2003 08:19 PM



Advertisments