Extracting output from system(3) call

Discussion in 'C Programming' started by v4vijayakumar, May 13, 2006.

  1. is there any standard way to extract output from system(3) function?
    TIA.
     
    v4vijayakumar, May 13, 2006
    #1
    1. Advertising

  2. if possible to use pipe(2) how could file descriptor can be derived
    from file pointer, FILE *. TIA.
     
    v4vijayakumar, May 13, 2006
    #2
    1. Advertising

  3. am going to use

    system("ls > /tmp/t");
     
    v4vijayakumar, May 13, 2006
    #3
  4. [OT]Re: Extracting output from system(3) call

    v4vijayakumar wrote:
    > is there any standard way to extract output from system(3) function?
    > TIA.
    >


    <OT>
    In a Posix system, use popen(3) instead
    </OT>

    Robert
     
    Robert Harris, May 13, 2006
    #4
  5. "v4vijayakumar" <> writes:
    > is there any standard way to extract output from system(3) function?
    > TIA.


    The C standard doesn't provide any way to do this, but your
    implementation might. Try a system-specific newsgroup.

    But first, please read <http://cfaj.freeshell.org/google/>.

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, May 13, 2006
    #5
  6. Re: [OT]Re: Extracting output from system(3) call

    Robert Harris wrote:
    > v4vijayakumar wrote:
    >
    >>is there any standard way to extract output from system(3) function?
    >>TIA.
    >>

    >
    >
    > <OT>
    > In a Posix system, use popen(3) instead
    > </OT>


    BTW: What is Posix???


    Best regards / Med venlig hilsen
    Martin Jørgensen

    --
    ---------------------------------------------------------------------------
    Home of Martin Jørgensen - http://www.martinjoergensen.dk
     
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, May 13, 2006
    #6
  7. v4vijayakumar

    SM Ryan Guest

    "v4vijayakumar" <> wrote:
    # is there any standard way to extract output from system(3) function?

    You have to save the output to a file within the command line,
    and then open the file subsequently.
    For example on unix, you can do something like
    if (system("cat gloober/* >fripper")!=0) error(...);
    FILE *fripper = fopen("fripper","r");
    ...
    fclose(fripper);

    POSIX systems also provide popen/pclose.
    FILE *fripper = popen("cat gloober/*","r");
    ...
    if (pclose(fripper)!=0) error(...);

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    But I do believe in this.
     
    SM Ryan, May 13, 2006
    #7
  8. v4vijayakumar

    Mike Wahler Guest

    Re: [OT]Re: Extracting output from system(3) call

    "Martin Jørgensen" <> wrote in message
    news:...
    > Robert Harris wrote:
    >> v4vijayakumar wrote:
    >>
    >>>is there any standard way to extract output from system(3) function?
    >>>TIA.
    >>>

    >>
    >>
    >> <OT>
    >> In a Posix system, use popen(3) instead
    >> </OT>

    >
    > BTW: What is Posix???


    http://en.wikipedia.org/wiki/POSIX

    -Mike
     
    Mike Wahler, May 13, 2006
    #8
  9. It seems that there is another way.

    fclose (stdout);
    stdout = fopen ("standard-output-file", "w");

    system("ls");

    /* process output file here */
     
    v4vijayakumar, May 15, 2006
    #9
  10. v4vijayakumar

    Flash Gordon Guest

    v4vijayakumar wrote:
    > It seems that there is another way.


    Another way to do what? Google is most definitely *not* Usenet, just an
    interface that dupes people in to making incorrect assumptions. Read
    http://clc-wiki.net/wiki/Intro_to_clc and provide context in future.

    > fclose (stdout);
    > stdout = fopen ("standard-output-file", "w");
    >
    > system("ls");
    >
    > /* process output file here */


    I've got no idea what you think the above will do, but I doubt it will
    actually do what you think. For a start, there is no guarantee that
    changing stdout within a program will affect where the output of a
    program invoked using system goes.
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
     
    Flash Gordon, May 15, 2006
    #10
  11. v4vijayakumar

    SM Ryan Guest

    "v4vijayakumar" <> wrote:
    # It seems that there is another way.
    #
    # fclose (stdout);
    # stdout = fopen ("standard-output-file", "w");
    #
    # system("ls");

    You're inferring a relation between a program's FILEs and
    the files of whatever kind of process system(...) uses.
    It's possible no such relation exists.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    Elvis was an artist. But that didn't stop him from joining the service
    in time of war. That's why he is the king, and you're a shmuck.
     
    SM Ryan, May 15, 2006
    #11
  12. stdin, stdout and stderr are 'FILE*' and they are associated to the
    particular program in execution. don't see any problems in fclose/fopen
    these streams.

    the following lines are from `man 3 stdin`.

    " Under normal circumstances every Unix program has three streams
    opened for it when it starts up, one for input, one for output, and one
    for printing diagnostic or error messages. These are typically attached
    to the user's terminal (see tty(4)) but might instead refer to files or
    other devices, depending on what the parent process chose to set up.
    (See also the ``Redirection'' section of sh(1) .)"
     
    v4vijayakumar, May 15, 2006
    #12
  13. v4vijayakumar

    Jordan Abel Guest

    On 2006-05-15, v4vijayakumar <> wrote:
    > stdin, stdout and stderr are 'FILE*' and they are associated to the
    > particular program in execution. don't see any problems in fclose/fopen
    > these streams.


    How do you propose to fopen stdin, when there is no FILE * parameter to
    fopen()?

    >
    > the following lines are from `man 3 stdin`.
    >
    > " Under normal circumstances every Unix program has three streams
    > opened for it when it starts up, one for input, one for output, and one
    > for printing diagnostic or error messages. These are typically attached
    > to the user's terminal (see tty(4)) but might instead refer to files or
    > other devices, depending on what the parent process chose to set up.
    > (See also the ``Redirection'' section of sh(1) .)"
    >
     
    Jordan Abel, May 15, 2006
    #13
  14. Jordan Abel wrote:
    > How do you propose to fopen stdin, when there is no FILE * parameter to
    > fopen()?


    #include <stdio.h>
    FILE *fopen(const char *path, const char *mode);
     
    v4vijayakumar, May 15, 2006
    #14
  15. v4vijayakumar

    Flash Gordon Guest

    v4vijayakumar wrote:

    Please provide context... oh wait, I already asked that. I'll assume you
    posted pefore seeing that message, but I know it has reached Google now
    so you have no further excuse for not providing context.

    > stdin, stdout and stderr are 'FILE*' and they are associated to the
    > particular program in execution. don't see any problems in fclose/fopen
    > these streams.


    Nothing wrong with closing them, potentially nothing wrong with opening
    it them on a file.

    > the following lines are from `man 3 stdin`.
    >
    > " Under normal circumstances every Unix program has three streams


    That should be your first clue that it is not necessarily true on all
    systems. Unix is not C so whatever is defined for Unix is defined for
    Unix and not necessarily all, or even all common, C implementations.

    > opened for it when it starts up, one for input, one for output, and one
    > for printing diagnostic or error messages. These are typically attached
    > to the user's terminal (see tty(4)) but might instead refer to files or
    > other devices, depending on what the parent process chose to set up.
    > (See also the ``Redirection'' section of sh(1) .)"


    What makes you think closing stdout/stdin/stderr and then opening it on
    another file actually redirects it for processes you launch with system.
    C streams are a high level concept and there is no reason to assume that
    opening stdout on a file will affect the low level stuff like the file
    descriptors 0, 1 and 2 that Unix uses, after all you have not told fopen
    that it is to be associated with one of those file descriptors!

    Note, discussion of file descriptors is off topic here, I merely mention
    them as one possible way things could be implemented.
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc

    Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
     
    Flash Gordon, May 15, 2006
    #15
  16. v4vijayakumar

    Flash Gordon Guest

    v4vijayakumar wrote:
    > Jordan Abel wrote:
    >> How do you propose to fopen stdin, when there is no FILE * parameter to
    >> fopen()?

    >
    > #include <stdio.h>
    > FILE *fopen(const char *path, const char *mode);


    I suspect that Jordan knows the prototype of fopen so I doubt that
    answers his question. The question being one for you to think about.
    consider the following code fragment:

    int i=1 ,j=4;
    int *p = &i;
    p = &j;

    What effect does the final statement have on i? What effect on j?
    Importantly, will j not be 1? That is the sort of thing you are trying
    to do.
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
     
    Flash Gordon, May 15, 2006
    #16
  17. v4vijayakumar wrote:
    >
    > It seems that there is another way.
    >
    > fclose (stdout);
    > stdout = fopen ("standard-output-file", "w");
    >
    > system("ls");
    >
    > /* process output file here */


    stdin/stdout/stderr are not l-values.

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, May 15, 2006
    #17
  18. v4vijayakumar

    Flash Gordon Guest

    Kenneth Brody wrote:
    > v4vijayakumar wrote:
    >> It seems that there is another way.
    >>
    >> fclose (stdout);
    >> stdout = fopen ("standard-output-file", "w");
    >>
    >> system("ls");
    >>
    >> /* process output file here */

    >
    > stdin/stdout/stderr are not l-values.


    C&V? For information, not because I disbelieve you. I thought that was
    the case, but when answering the question I tried to find that in the
    standard but couldn't.
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
     
    Flash Gordon, May 15, 2006
    #18
  19. v4vijayakumar

    Kevin Bagust Guest

    Flash Gordon wrote:
    > Kenneth Brody wrote:
    >> v4vijayakumar wrote:
    >>> It seems that there is another way.
    >>>
    >>> fclose (stdout);
    >>> stdout = fopen ("standard-output-file", "w");
    >>>
    >>> system("ls");
    >>>
    >>> /* process output file here */

    >>
    >> stdin/stdout/stderr are not l-values.

    >
    > C&V? For information, not because I disbelieve you. I thought that was
    > the case, but when answering the question I tried to find that in the
    > standard but couldn't.


    I can only find it mentioned in footnote 230 in C99, which is associated
    with the description of the freopen function (7.19.5.4). The foot note says:

    The primary use of the freopen is to change the file associated with a
    standard text stream (stderr,stdin, or stdout), as those identifiers
    need not be modifiable lvalues to which the value returned by the fopen
    function may be assigned.

    Kevin.
     
    Kevin Bagust, May 15, 2006
    #19
    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. 131.96.106.128 [johnny]

    grab system() call output

    131.96.106.128 [johnny], Jul 24, 2003, in forum: C++
    Replies:
    1
    Views:
    451
    Victor Bazarov
    Jul 24, 2003
  2. Wes Gamble
    Replies:
    2
    Views:
    97
    Robert Klemme
    Jun 19, 2009
  3. Jerry Mr
    Replies:
    10
    Views:
    208
    Brian Candler
    Oct 8, 2009
  4. Madhu Ramachandran

    output of system() call

    Madhu Ramachandran, Jan 4, 2006, in forum: Perl Misc
    Replies:
    5
    Views:
    90
    zentara
    Jan 5, 2006
  5. alfonsobaldaserra

    extracting values from vmstat output

    alfonsobaldaserra, Feb 18, 2009, in forum: Perl Misc
    Replies:
    8
    Views:
    155
    Ashish Kumar
    Feb 20, 2009
Loading...

Share This Page