By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,007 Members | 1,045 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,007 IT Pros & Developers. It's quick & easy.

need help getting the results of the system function into a variable

P: n/a
SF
Hello All,

In a windows C learning project I am wokring on I use the system
function to run a command, I want to suck the results into a vairable.
The system function seems to only return an int. I would like to know
how could this is done? I can see only one way currently and I know
that there must be a better method. Right now I would be tempted to
pipe the output of the command to a temp file and then get the data
from the file. That seems really dirty to me, can you suggest a better
way?

Much thanks in advance.

Feb 27 '06 #1
Share this Question
Share on Google+
22 Replies


P: n/a
In article <11**********************@z34g2000cwc.googlegroups .com>,
SF <so***********@gmail.com> wrote:
In a windows C learning project I am wokring on I use the system
function to run a command, I want to suck the results into a vairable.
The system function seems to only return an int. I would like to know
how could this is done?
There is no way to do it using only standard C.
I can see only one way currently and I know
that there must be a better method. Right now I would be tempted to
pipe the output of the command to a temp file and then get the data
from the file. That seems really dirty to me, can you suggest a better
way?


pipes are operating system-specific extensions, outside the
control of standard C. Standard C does not even define the result
of using system(): it just says that it exists and that the result
is operating-system dependant.

Possibly you meant "redirect" instead of "pipe", but the ability to
redirect system() output into a file is again operating-system dependant
(though it's probably available on more operating systems than support
pipes.)

For what it's worth, the usual POSIX of handling this is to use
popen(), but if you want to know what facilities are available in
Windows, you should check a Windows programming newsgroup.

--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Feb 27 '06 #2

P: n/a
SF
Thanks for the reply,

I am using redirect rather than a pipe. I guess I did not use the right
term there.

Are you aware of a better function then system() to run a shell
command? one that could return the output rather than an int.

Thanks for your help.

Feb 27 '06 #3

P: n/a
In article <11**********************@t39g2000cwt.googlegroups .com>,
SF <so***********@gmail.com> wrote:
Are you aware of a better function then system() to run a shell
command? one that could return the output rather than an int.


As I indicated, system() is all you get in standard C, and anything
else should be asked about in a newsgroup specific to your
operating system. When you post there, be sure to specify your
version of Windows and Service Pack(s), as the answer might be
dependant upon exactly what you have installed.
--
There are some ideas so wrong that only a very intelligent person
could believe in them. -- George Orwell
Feb 27 '06 #4

P: n/a

"SF" <so***********@gmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
Hello All,

In a windows C learning project I am wokring on I use the system
function to run a command, I want to suck the results into a vairable.
The system function seems to only return an int. I would like to know
how could this is done? I can see only one way currently and I know
that there must be a better method. Right now I would be tempted to
pipe the output of the command to a temp file and then get the data
from the file. That seems really dirty to me, can you suggest a better
way?

Much thanks in advance.

If you have a POSIX-compliant system, use popen()
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Feb 27 '06 #5

P: n/a
SF wrote:
Are you aware of a better function then system() to run a shell
command? one that could return the output rather than an int.


Anything else would be platform specific. You need to find a newsgroup
dedicated to your implementation or (perhaps) OS. Also, see the
information in my .sig below.
Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Feb 27 '06 #6

P: n/a
> Are you aware of a better function then system() to run a shell
command? one that could return the output rather than an int.


Have you tried CreateProcess system call to do so? I know that this
system call returns several information about the process that you
start with it. This is something specific for Windows platform and is
not STDC at all.

Check this link:
http://msdn.microsoft.com/library/de..._processes.asp

This has several information about creating processes on windows
platforms.

Hope that it helps.

Ron

Feb 28 '06 #7

P: n/a
"Ron Lima" <ro******@gmail.com> writes:
Are you aware of a better function then system() to run a shell
command? one that could return the output rather than an int.


Have you tried CreateProcess system call to do so? I know that this
system call returns several information about the process that you
start with it. This is something specific for Windows platform and is
not STDC at all.

[snip]

And if this information had been posted to a Windows-specific
newsgroup, the experts there could post any necessary corrections.
As it is, most of us have no idea whether the suggested solution will
actually work, or whether something else might be better.

If you want to discuss Windows-specific interfaces, please don't do it
here.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 28 '06 #8

P: n/a
Its execpls( ) ... this works well on unix operating systems...

Jitendra

Feb 28 '06 #9

P: n/a
sorry .... its execlp ( ) ...

thanks
jitendra

Feb 28 '06 #10

P: n/a
ji*********@gmail.com writes:
sorry .... its execlp ( ) ...


And it's off-topic; try comp.unix.programmer.

And please read <http://cfaj.freeshell.org/google/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 28 '06 #11

P: n/a
SF

Keith Thompson wrote:
ji*********@gmail.com writes:
sorry .... its execlp ( ) ...


And it's off-topic; try comp.unix.programmer.

And please read <http://cfaj.freeshell.org/google/>.

--

Thanks for all the suggestions, I have posted this in a win32
programming groups and they suggested something similar.

Sorry if this was off topic as a few members seem to indicate, I
thought this was a general C discussion forum rather than related to
any particular OS. I was not aware that my topic was outside the scope
of standard C development.

BTW, what is the topic of this group so I can use it better?
For those who suggested the createprocess function, I have seen that on
msdn but I have not found a working example source. I am still
learning, if you could show me an example or link to one it would help
me a lot.

Thank you, sorry again for the O/T.

Feb 28 '06 #12

P: n/a

SF wrote:
Keith Thompson wrote:
ji*********@gmail.com writes:
sorry .... its execlp ( ) ...
And it's off-topic; try comp.unix.programmer.

And please read <http://cfaj.freeshell.org/google/>.

--

Thanks for all the suggestions, I have posted this in a win32
programming groups and they suggested something similar.

Sorry if this was off topic as a few members seem to indicate, I
thought this was a general C discussion forum rather than related to
any particular OS. I was not aware that my topic was outside the scope
of standard C development.


This group is specifically /not/ about any particular OS, rather it
concerns only C language as defined by the Standard.
BTW, what is the topic of this group so I can use it better?


A good place to start is http://www.clc-wiki.net/wiki/Main_Page
Also, lurking for a while helps a lot (you can use Google to quick-lurk
a few months worth of posts).

--
BR, Vladimir

Feb 28 '06 #13

P: n/a
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
ji*********@gmail.com writes:
sorry .... its execlp ( ) ...
And it's off-topic; try comp.unix.programmer.


Besides being off-topic it isn't the right answer for any version
of Windows -or- Unix.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Feb 28 '06 #14

P: n/a
SF

SF wrote:
Keith Thompson wrote:
ji*********@gmail.com writes:
sorry .... its execlp ( ) ...


And it's off-topic; try comp.unix.programmer.

And please read <http://cfaj.freeshell.org/google/>.

--

Thanks for all the suggestions,
For those who suggested the createprocess function, I have seen that on
msdn but I have not found a working example source. I am still
learning, if you could show me an example or link to one it would help
me a lot.

Thank you, sorry again for the O/T.


The example here works for me however I am not certain how I can get
the results into a variable.

http://msdn.microsoft.com/library/de..._processes.asp

I would like to be able to run a command line program and get the
results. Not simply process information but the output. Any command
that would output text then exit such as netstat, nslookup or even dir.
I know these questions are windows specific, but I think that it is a C
realted question on how to interface with a function and get its info
into a variable. This is more of a learning project for me rather than
trying to solve any particular problem.

I am still way off topic, I apologize.

SF

Feb 28 '06 #15

P: n/a

SF wrote:

<snip "way off topic" stuff>
I am still way off topic, I apologize.


So why come back here, and not go to one of the groups dealing with
your specific platform?

--
BR, Vladimir

Feb 28 '06 #16

P: n/a
SF wrote:
I know these questions are windows specific, but I think that it is a C
realted question on how to interface with a function and get its info
into a variable.


This is on topic, but if you have to ask this sort of question, you're
better off re-reading your C textbook. Here's an example, though:

int a_func(void)
{
return 42;
}

void b_func(int *x)
{
*x = 42;
return;
}

int main(void)
{
int a_variable;

a_variable = a_func();

b_func(&a_variable);

return 0;
}

If you're asking about a specific non-standard function, you are off
topic here.

--
BR, Vladimir

Feb 28 '06 #17

P: n/a
SF

Vladimir S. Oka wrote:
SF wrote:

<snip "way off topic" stuff>
I am still way off topic, I apologize.


So why come back here, and not go to one of the groups dealing with
your specific platform?

--
BR, Vladimir


Well, since I already have an off topic post where people are nice
enough to reply, adding something to that post is not the worst thing
in the world is it?

I do have this posted in other groups. I also said how it could be
viewed as a regular C question since I am not really asking anything
about windows itself but on using a popular API written in C.

Also I figure that my on subject addition to an off topic thread is as
harmless as your off subject question in that same thread.

Cheers.

Feb 28 '06 #18

P: n/a
In article <11**********************@i40g2000cwc.googlegroups .com>,
SF <so***********@gmail.com> wrote:
I would like to be able to run a command line program and get the
results. Not simply process information but the output. Any command
that would output text then exit such as netstat, nslookup or even dir.
I know these questions are windows specific, but I think that it is a C
realted question on how to interface with a function and get its info
into a variable.


The C way to "interface with a function and get its info into a
variable" is to store the return value of the function. If the
information you need is not in the return value of the function
(e.g. because you want the I/O output performed by the function)
then the matter is no longer a C matter but a system-specific matter.
I will have some pity on you and let you know that there isn't
any standard Unix or Windows call equivilent in structure to

variable := result_of_command("command string");

You could synthesize such a routine yourself, but watch out for the
possibility that the output is gigabytes long or even indefinitely
long...

The calls that are available all either do not capture the output
(not what you want) or else capture it in a form that you have to fread()
or read().

Older versions of Windows literally did do what you've been trying to
avoid -- redirected the output to a file and read the file back. Newer
versions of Windows provide Windows-specific mechanisms that skip
actually writing the output to disk. But those are topics for
Windows-specific newsgroups.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Feb 28 '06 #19

P: n/a
On 28 Feb 2006 07:28:06 -0800, "SF" <so***********@gmail.com> wrote:
Sorry if this was off topic as a few members seem to indicate, I
thought this was a general C discussion forum rather than related to
any particular OS. I was not aware that my topic was outside the scope
of standard C development.

BTW, what is the topic of this group so I can use it better?


The topic is the C language itself, not any particular implementation
or platform.

Your original question was fine (Is there a way to do it?) and
Walter's original response was correct (Not in standard C). Walter was
also correct (and helpful) in suggesting a different newsgroup for
information about your specific problem.

Things went off-topic from there.

--
Al Balmer
Sun City, AZ
Feb 28 '06 #20

P: n/a
SF wrote:

Vladimir S. Oka wrote:
SF wrote:

<snip "way off topic" stuff>
> I am still way off topic, I apologize.
So why come back here, and not go to one of the groups dealing with
your specific platform?

--
BR, Vladimir


Well, since I already have an off topic post where people are nice
enough to reply, adding something to that post is not the worst thing
in the world is it?


Being off topic is being off topic is being off topic...
I do have this posted in other groups. I also said how it could be
viewed as a regular C question since I am not really asking anything
about windows itself but on using a popular API written in C.
I actually have answered the on topic part of your question, the way I
understood it. I may not have understood the question in the first
place, though.
Also I figure that my on subject addition to an off topic thread is as
harmless as your off subject question in that same thread.


I honestly don't know what question of mine you're referring to (my
memory is a fallible device). Can you quote it?

As I already said, the on topic part I have replied to. Even if I wanted
I couldn't help you with the system specific bit (and that is really
the gist of the question), as I generally don't program for hosted
implementations (not for a living anyway).

--
BR, Vladimir

Nirvana? That's the place where the powers
that be and their friends hang out.
-- Zonker Harris

Feb 28 '06 #21

P: n/a
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote:
In article <11**********************@i40g2000cwc.googlegroups .com>,
SF <so***********@gmail.com> wrote:
I would like to be able to run a command line program and get the
results. Not simply process information but the output. Any command
that would output text then exit such as netstat, nslookup or even dir.
I know these questions are windows specific, but I think that it is a C
realted question on how to interface with a function and get its info
into a variable.
The C way to "interface with a function and get its info into a
variable" is to store the return value of the function. If the
information you need is not in the return value of the function


Not necessarily. Some functions "return" multiple values through
pointers passed to them. E.g., fgets() writes the text it reads through
its first argument (and returns an error indicator); strtol() does the
opposite, returning the value it converted, and writing the address
where it stopped converting (one use for which is as an error indicator)
through its second argument.
(e.g. because you want the I/O output performed by the function)


In this specific case, though, you're quite right.

Richard
Mar 1 '06 #22

P: n/a
In article <44****************@news.xs4all.nl>,
Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote:
The C way to "interface with a function and get its info into a
variable" is to store the return value of the function. If the
information you need is not in the return value of the function

Not necessarily. Some functions "return" multiple values through
pointers passed to them. E.g., fgets() writes the text it reads through
its first argument (and returns an error indicator); strtol() does the
opposite, returning the value it converted, and writing the address
where it stopped converting (one use for which is as an error indicator)
through its second argument.


If a function returns a value through a pointer passed into it,
then the function is *putting* the value, which is not compatible
with the user requirement to *get* its information into a variable.
Passive versus active.
--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
Mar 1 '06 #23

This discussion thread is closed

Replies have been disabled for this discussion.