473,226 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

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

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
22 2784
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
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
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

"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
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
> 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
"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
Its execpls( ) ... this works well on unix operating systems...

Jitendra

Feb 28 '06 #9
sorry .... its execlp ( ) ...

thanks
jitendra

Feb 28 '06 #10
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
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

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

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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
11
by: Jim | last post by:
Hi, I keep getting form results emailed to me that would indicate a form from my web site is getting submitted with all fields blank or empty, but my code should preventing users from proceeding...
4
by: Bob | last post by:
Below is sample code that illustrates what I'm trying to do. For sake of brevity I didn't include the properties of buildBtn that determine what data to request. The problem is I never see...
3
by: Mike | last post by:
Hey guys I am pulling my hair out on this problem!!!!! Any help or ideas or comments on how to make this work I would be grateful! I have been working on this for the past 4 days and nothing I do...
5
by: MFC | last post by:
Ok, after three C# books, (C# How to Program, Programming in the Key of C#, and C# Weekend Crash Course) and three weeks, I believe I have tried everything to make a certain form function...
5
by: Mad Scientist Jr | last post by:
I'm trying to record a voice memo on a Pocket PC (2000) from VB.NET (smart device application from vs.net 2003). Tom Shelton did an initial conversion to VB.NET which I am trying to run, but am...
3
by: sunbeam | last post by:
Short Description of the Project: we developed a e-learning system for our students. each student has a unique username/password to view the modules he/she should view and nothing more. since we...
3
by: dedalusenator | last post by:
Hello Folks, My first posting here and I am a stuck in figuring out the exact way to update a global variable from within a function that doesnt return any value (because the function is a...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

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.