Read output from system( ).

Discussion in 'C Programming' started by SR, Oct 5, 2003.

  1. SR

    SR Guest

    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
     
    SR, Oct 5, 2003
    #1
    1. Advertising

  2. SR

    Artie Gold Guest

    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.
     
    Artie Gold, Oct 5, 2003
    #2
    1. Advertising

  3. Artie Gold <> 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)
     
    Floyd Davidson, Oct 5, 2003
    #3
  4. SR

    CBFalconer Guest

    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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Oct 5, 2003
    #4
  5. 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 :
    "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
     
    Richard Heathfield, Oct 5, 2003
    #5
  6. 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 :
    "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
     
    Richard Heathfield, Oct 5, 2003
    #6
  7. Richard Heathfield <> 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)
     
    Floyd Davidson, Oct 5, 2003
    #7
  8. On 04 Oct 2003 20:07:33 -0800
    Floyd Davidson <> 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#;+,)/'///*");}
     
    Pieter Droogendijk, Oct 5, 2003
    #8
  9. SR

    CBFalconer Guest

    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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Oct 5, 2003
    #9
  10. CBFalconer <> 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)
     
    Floyd Davidson, Oct 5, 2003
    #10
  11. SR

    Kevin Easton Guest

    Floyd Davidson <> wrote:
    > Richard Heathfield <> 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.)


    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.
     
    Kevin Easton, Oct 6, 2003
    #11
  12. Kevin Easton <> wrote:
    >Floyd Davidson <> wrote:
    >> Richard Heathfield <> 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.


    ....

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

    --
    Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
    Ukpeagvik (Barrow, Alaska)
     
    Floyd Davidson, Oct 6, 2003
    #12
  13. Kevin Easton wrote:

    > Floyd Davidson <> wrote:
    >> Richard Heathfield <> 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.)

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

    --
    Richard Heathfield :
    "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
     
    Richard Heathfield, Oct 6, 2003
    #13
  14. SR

    Micah Cowan Guest

    Floyd Davidson <> writes:

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


    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
     
    Micah Cowan, Oct 6, 2003
    #14
  15. SR

    Kevin Easton Guest

    Floyd Davidson <> wrote:
    > Kevin Easton <> wrote:
    >>Floyd Davidson <> wrote:
    >>> Richard Heathfield <> 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.

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


    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.
     
    Kevin Easton, Oct 6, 2003
    #15
  16. Kevin Easton <> wrote:
    >
    >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?

    --
    Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
    Ukpeagvik (Barrow, Alaska)
     
    Floyd Davidson, Oct 6, 2003
    #16
  17. SR

    Dan Pop Guest

    In <blojih$mes$> Richard Heathfield <> writes:

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

    >
    >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
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Oct 6, 2003
    #17
  18. SR

    Dan Pop Guest

    In <> Floyd Davidson <> writes:

    >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
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Oct 6, 2003
    #18
  19. Dan Pop wrote:
    > In <blojih$mes$> Richard Heathfield writes:
    >>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 ?

    >>
    >>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()?


    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.

    --
    Richard Heathfield :
    "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
     
    Richard Heathfield, Oct 7, 2003
    #19
  20. SR

    Mike Wahler Guest

    "Richard Heathfield" <> wrote in message
    news:bltd0e$grl$...
    > Dan Pop wrote:
    > > In <blojih$mes$> Richard Heathfield writes:
    > >>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 ?
    > >>
    > >>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()?

    >
    > 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
     
    Mike Wahler, Oct 7, 2003
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Bentley

    Output / Debug window output bug?

    John Bentley, Sep 10, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    601
    John Bentley
    Sep 10, 2003
  2. chuck amadi
    Replies:
    1
    Views:
    499
    Larry Bates
    Jun 23, 2004
  3. Paul
    Replies:
    1
    Views:
    438
  4. jmr
    Replies:
    2
    Views:
    345
  5. Yin Zhu
    Replies:
    15
    Views:
    970
    Richard Bos
    Sep 12, 2007
Loading...

Share This Page