473,473 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

initialize a string variable with output of a command?

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

Jul 19 '05 #1
3 8015
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.

Jul 19 '05 #2
In article <EG*******************@twister.rdc-kc.rr.com>, no@mail.com
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.
Jul 19 '05 #3
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
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Erik Andersson | last post by:
Hi! <? class Foo { function getMessage() { return "foo"; } } class Bar { function getFoo() {
4
by: Mark Wilson CPU | last post by:
This must be easy, but I'm missing something... I want to execute a Perl script, and capture ALL its output into a PHP variable. Here are my 2 files: -------------------------------------...
74
by: Peter | last post by:
Hi, So many times, I have seen compile warning: "you used a char* without initilize it", probably on the code like this: ------------ char* ptr; func(..., ptr); ----------
19
by: moxm | last post by:
I have a statement declares a globle variable like this : char *pname = NULL; Then I used splint to check the code, I got errors: err.c:8:15: Global pname initialized to null value: pname =...
15
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have ...
5
by: qazwart | last post by:
I am reading from a "cvs rlog" command, and I need both the STDOUT and STDERR. Unfortunately, something very strange is happening. If I do this: $cmd = "$cvs_cmd -q rlog -NS...
14
by: rtillmore | last post by:
Hello, I did a quick google search and nothing that was returned is quite what I am looking for. I have a 200 character hexadecimal string that I need to convert into a 100 character string. ...
1
by: TP | last post by:
Hi everybody, I try to find a quick way to redirect the standard output of a Python command (for example: print "message") to a python variable "foobar". Ok, in this simple example, I could do...
5
by: Timothy Madden | last post by:
Hy static members of non-integral type need to be declared in the class, but defined (and constructed or initialized) outside the class. Like this class SystemName { public:
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.