Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Read output from system( ).

Reply
Thread Tools

Read output from system( ).

 
 
SR
Guest
Posts: n/a
 
      10-05-2003
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

 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      10-05-2003
SR wrote:
> 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:

news:comp.unix.programmer

and for Linux specific questions use:

news:comp.os.linux.development.apps

HTH,
--ag

--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

 
Reply With Quote
 
 
 
 
Floyd Davidson
Guest
Posts: n/a
 
      10-05-2003
Artie Gold <(E-Mail Removed)> wrote:
>SR wrote:
>> 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:


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.

>news:comp.unix.programmer
>
>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.

--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      10-05-2003
SR wrote:
>
> 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.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      10-05-2003
Floyd Davidson wrote:

<snip>

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

--
Richard Heathfield : (E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      10-05-2003
SR wrote:

> 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)
{
.
.
.

--
Richard Heathfield : (E-Mail Removed)
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
 
Reply With Quote
 
Floyd Davidson
Guest
Posts: n/a
 
      10-05-2003
Richard Heathfield <(E-Mail Removed)> wrote:
>Floyd Davidson wrote:
>
><snip>
>
>> 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.


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

--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) (E-Mail Removed)
 
Reply With Quote
 
Pieter Droogendijk
Guest
Posts: n/a
 
      10-05-2003
On 04 Oct 2003 20:07:33 -0800
Floyd Davidson <(E-Mail Removed)> wrote:
<snip>
> 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.

> >news:comp.unix.programmer
> >
> >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.


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.

--
char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
c,k,0),x(c,k+=*s-c,s+1),*k)x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      10-05-2003
Floyd Davidson wrote:
>

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

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
Floyd Davidson
Guest
Posts: n/a
 
      10-05-2003
CBFalconer <(E-Mail Removed)> wrote:
>Floyd Davidson wrote:
>>

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


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.

--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) (E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange problem - page output contains output from another request Paul ASP .Net 1 04-10-2007 03:41 PM
parse output screen ok but cant get desired output new file! chuck amadi Python 1 06-23-2004 02:16 PM
Sony Precision Cinema Progressive Output vs Component 480p Output Otto Pylot DVD Video 1 04-18-2004 09:49 PM
Is Fuji S3000 3.2m/pixel output, or 6 m/pixel interpolated output? Peter H Digital Photography 43 12-04-2003 02:35 PM
Output / Debug window output bug? John Bentley ASP .Net 0 09-10-2003 07:38 AM



Advertisments