Read output from system( ).

S

SR

Hi,
I need to read the output from a system( ) function within a C program.
The function however only returns the exit status.
How can I read what system( ) sends to stdout ?
(is there a simpler way than having to fork a child process and then
catching its output ?)
(if needed: working on linux)

Thanks
 
A

Artie Gold

SR said:
Hi,
I need to read the output from a system( ) function within a C program.
The function however only returns the exit status.

Right, that what it does.
How can I read what system( ) sends to stdout ?
(is there a simpler way than having to fork a child process and then
catching its output ?)

Not in standard C.
(if needed: working on linux)

If relevant, your question is off topic.

<OT>
Just so you don't `gorge out' (a `one time only' offer):

man popen
</OT>

In the future, please post only questions about standard C here, For
general Unix questions use:


and for Linux specific questions use:


HTH,
--ag
 
F

Floyd Davidson

Artie Gold said:
Right, that what it does.


Not in standard C.


If relevant, your question is off topic.

<OT>
Just so you don't `gorge out' (a `one time only' offer):

man popen
</OT>

In the future, please post only questions about standard C here, For
general Unix questions use:

If the OP didn't know the answer, how would he know there was no
Standard C answer but that a Unix answer does exist?

Regardless, there is a Standard C answer, which just doesn't
happen to be as nice as the unix popen() method. The command
given to the system() function can redirect stdout to a file.
The program then simply reads the file.
and for Linux specific questions use:

news:comp.os.linux.development.apps

Incidentally, for the OP, both of those groups are probably
better places to ask "programming" questions of any kind.
C.l.c is for C lawyering, not C programming.
 
C

CBFalconer

SR said:
I need to read the output from a system( ) function within a C
program. The function however only returns the exit status. How
can I read what system( ) sends to stdout ? (is there a simpler
way than having to fork a child process and then catching its
output ?) (if needed: working on linux)

fork? child? process? linux? None of these are defined in the C
standard, and thus you are off-topic here. What system() actually
does is also system dependant. So you need a group dealing with
your system, which apparently is linux. Use that and "unix" to
search for a suitable group.
 
R

Richard Heathfield

Floyd Davidson wrote:

C.l.c is for C lawyering, not C programming.

If that has become true, it's a sad day for Usenet. I prefer to think of clc
as being for programming in C, rather than programming for a platform. C
lawyering really belongs in csc, IMHO.
 
R

Richard Heathfield

SR said:
Hi,
I need to read the output from a system( ) function within a C program.
The function however only returns the exit status.
How can I read what system( ) sends to stdout ?

system("foo > filename.txt");
fp = fopen("filename.txt", r");
if(fp != NULL)
{
.
.
.
 
F

Floyd Davidson

Richard Heathfield said:
Floyd Davidson wrote:



If that has become true, it's a sad day for Usenet. I prefer to think of clc
as being for programming in C, rather than programming for a platform. C
lawyering really belongs in csc, IMHO.

C.s.c is for C legislating. ;-)

Then the lawyers get it, and that's here.

Incidentally, I don't see that separation as a sad day. There
really is a distinct separation in the three areas. (I don't
get a negative connotation from "lawyering" either, and that may
be the only difference in what we mean.)
 
P

Pieter Droogendijk

On 04 Oct 2003 20:07:33 -0800
Floyd Davidson said:
If the OP didn't know the answer, how would he know there was no
Standard C answer but that a Unix answer does exist?

Had he consulted the FAQ before posting, which he should have done in the first
place, he would have known all he needed to know about topical posting. He would
also not have posted this question here, or in any other newsgroup, since the
question is answered in the FAQ: http://www.eskimo.com/~scs/C-faq/q19.30.html
Regardless, there is a Standard C answer, which just doesn't
happen to be as nice as the unix popen() method. The command
given to the system() function can redirect stdout to a file.
The program then simply reads the file.

And hope your implementation supports system() functionality (tested with
system(NULL)), i/o redirection (how would one test that?) and files (of course
you wouldn't hard-code a path... would you?)
Although it would work on MOST systems, the question is not answered in 'chapter
19: System Dependencies' for nothing.
Incidentally, for the OP, both of those groups are probably
better places to ask "programming" questions of any kind.
C.l.c is for C lawyering, not C programming.

Correction, they're better places to ask questions about programming on UNIX and
Linux platforms, respectively. This question would have been more topical there
indeed, and most likely future questions from the OP as well.
 
C

CBFalconer

Floyd said:
.... snip ...

Regardless, there is a Standard C answer, which just doesn't
happen to be as nice as the unix popen() method. The command
given to the system() function can redirect stdout to a file.
The program then simply reads the file.

That is not a standard C answer. C says nothing about what
system() actually does, and such things as executing programs and
redirection are system and implementation dependant. Your
solution can be expected to work on unix/linux/msdos, but probably
not under OS360 or MPE on the HP3000.
 
F

Floyd Davidson

CBFalconer said:
That is not a standard C answer. C says nothing about what
system() actually does, and such things as executing programs and
redirection are system and implementation dependant. Your
solution can be expected to work on unix/linux/msdos, but probably
not under OS360 or MPE on the HP3000.

So? That hardly keeps it from being a Standard C answer.

After all, the standard doesn't require there be anything
connected to stdout, and on some platforms using printf()
results in a no-op. Same with system() in this case.

The point is, the C code does not violate the C Standard.
 
K

Kevin Easton

Floyd Davidson said:
C.s.c is for C legislating. ;-)

Then the lawyers get it, and that's here.

Incidentally, I don't see that separation as a sad day. There
really is a distinct separation in the three areas. (I don't
get a negative connotation from "lawyering" either, and that may
be the only difference in what we mean.)

I think Richard might have been referring to the "and not C programming"
bit. The key is realising that comp.lang.c is for C programming - but
not for programming-to-APIs-that-happen-to-have-a-C-binding.

By the way, I don't think it's offtopic at all to ask "can I read the
standard output of a program I start with system()" - it's just that the
answer is "no", or more verbosely, "no, unless you can rely on running
on a particular subset of C-supporting systems, so you might want to ask
in a newsgroup dedicated to programming on your particular system, to
see if it's one of them".

- Kevin.
 
F

Floyd Davidson

Kevin Easton said:
....

By the way, I don't think it's offtopic at all to ask "can I read the
standard output of a program I start with system()" - it's just that the
answer is "no", or more verbosely, "no, unless you can rely on running
on a particular subset of C-supporting systems, so you might want to ask
in a newsgroup dedicated to programming on your particular system, to
see if it's one of them".

See what I mean about C lawyering! A great example, thank you.

Many programs "rely on running on a particular subset of
C-supporting systems". We aren't going to say "no, unless"
every time anyone includes stdio.h and uses any function
declared therein. Your point is not significant, and your
initial "no" based on it is incorrect.

We agree on everything else, hence, the correct response is
"Yes, but ..."
 
R

Richard Heathfield

Kevin said:
I think Richard might have been referring to the "and not C programming"
bit.

Indeed he was (and I should know).
The key is realising that comp.lang.c is for C programming - but
not for programming-to-APIs-that-happen-to-have-a-C-binding.

Precisely. We have no more questions for this witness, m'lud.

<snip>
 
M

Micah Cowan

Floyd Davidson said:
So? That hardly keeps it from being a Standard C answer.

Doesn't it? If the answer has nothing to do with Standard C, it
is not a Standard C answer.
After all, the standard doesn't require there be anything
connected to stdout, and on some platforms using printf()
results in a no-op. Same with system() in this case.

The point is, the C code does not violate the C Standard.

That has nothing to do with whether your answer is a "Standard C
answer". In particular, your assertion that "the command given to
the system() function can redirect stdout to a file" may or may
not be true: the Standard has nothing to say about whether this
is actually possible, so it is assuredly not a Standard C answer.

-Micah
 
K

Kevin Easton

Floyd Davidson said:
See what I mean about C lawyering! A great example, thank you.

Many programs "rely on running on a particular subset of
C-supporting systems". We aren't going to say "no, unless"
every time anyone includes stdio.h and uses any function
declared therein. Your point is not significant, and your
initial "no" based on it is incorrect.

We agree on everything else, hence, the correct response is
"Yes, but ..."

In that case the answer to *every* question is "Yes, but.." and thus
you could ask everything from "How do I turn off my combine harvester"
to "Why don't my ailerons deflect far enough."

"I'm starting Excel from system(). What is the correct string to use to
make it use European dates?" Now, is that a question about C, just
because it involves a C function? Or is it an Excel question? The same
applies to "What is the correct string to use in system() to make it
send the standard output of a program to a file?" - it's a question
about your shell, not about C.

Being within earshot of C does not make something C.

- Kevin.
 
F

Floyd Davidson

Kevin Easton said:
In that case the answer to *every* question is "Yes, but.." and thus
you could ask everything from "How do I turn off my combine harvester"
to "Why don't my ailerons deflect far enough."

"I'm starting Excel from system(). What is the correct string to use to
make it use European dates?" Now, is that a question about C, just
because it involves a C function? Or is it an Excel question? The same
applies to "What is the correct string to use in system() to make it
send the standard output of a program to a file?" - it's a question
about your shell, not about C.

Being within earshot of C does not make something C.

- Kevin.

Did you have any comments that related to either C programming
or to C lawyering?
 
D

Dan Pop

In said:
system("foo > filename.txt");
fp = fopen("filename.txt", r");

Where does the standard say that, if the fopen call succeeds, the
stream contains the output of the command executed by system()?

Dan
 
D

Dan Pop

In said:
The point is, the C code does not violate the C Standard.

Nope, the point is, the C standard doesn't say the code will have the
effect intended by the OP. From the point of view of the C standard,
system("foo >out.txt") and popen("foo", "r") are equally implementation
specific solutions (it is not a standard violation to call a function
called popen), that may or may not work on a conforming C implementation.

The only difference is that one may fail at run time and the other
in translation phase 8.

Dan
 
R

Richard Heathfield

Dan said:
Where does the standard say that, if the fopen call succeeds, the
stream contains the output of the command executed by system()?

For a moment I forgot where I was, and I accidentally gave a pragmatic reply
that would actually help the OP out of his current difficulty. I do
apologise - to you, to the OP, and indeed to the whole group. I'll try not
to make the same mistake again.
 
M

Mike Wahler

Richard Heathfield said:
For a moment I forgot where I was, and I accidentally gave a pragmatic reply
that would actually help the OP out of his current difficulty. I do
apologise - to you, to the OP, and indeed to the whole group. I'll try not
to make the same mistake again.

Despite my 'reputation' as an officer of the 'topic police',
I'd like to say that your reply imo would be perfectly
acceptable were it prefaced, e.g.

"Many operating systems have the facility to 'redirect'
the output of a command to a file. *For example*, some
OS's, e.g. MSDOS/Windows/Unix use the '>' character".

Then your example code, perhaps followed by "check your
OS documentation to be sure."

So imo you simply "started talking before your mouth was
completely open". :) And of course, given your history
here, I certainly forgive you. :)

Oh, and (my desperate attempt to return to topicality):
you forgot the opening quote on 'fopen()'s "r" argument.
Now go write a 'for' loop on the blackboard. :)

I suppose someone will flame me for saying all this, but it's
getting chilly outside, so...

-Mike
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top