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

S

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.
 
W

Walter Roberson

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.
 
S

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.
 
W

Walter Roberson

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.
 
F

Fred Kleinschmidt

SF said:
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()
 
D

Default User

SF said:
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
 
R

Ron Lima

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/default.asp?url=/library/en-us/dllproc/base/child_processes.asp

This has several information about creating processes on windows
platforms.

Hope that it helps.

Ron
 
K

Keith Thompson

Ron Lima said:
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.
 
S

SF

Keith said:
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.
 
V

Vladimir S. Oka

SF said:
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).
 
S

SF

SF said:
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/default.asp?url=/library/en-us/dllproc/base/creating_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
 
V

Vladimir S. Oka

SF said:
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.
 
S

SF

Vladimir said:
SF wrote:



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

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.
 
W

Walter Roberson

SF said:
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.
 
A

Al Balmer

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.
 

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
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top