Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: parse output of shell command in c++

  1. #1
    Join Date
    Oct 2006
    Beans
    1

    parse output of shell command in c++

    I'd like my c++ program to execute a shell command and then evaluate the results, like e. g.

    execute "ls" and save the results in an variable. The system() command does not work since it echoes the results of the command to stdout.

    Any ideas?

    Regards,
    Lotharster

  2. #2
    Join Date
    Nov 2004
    Location
    Clemson, SC
    Beans
    271

    Re: parse output of shell command in c++

    popen gives you a FILE * of the stdout.

  3. #3
    Join Date
    Nov 2005
    Location
    Toronto
    Beans
    1,181

    Re: parse output of shell command in c++

    In case you need more detail, here is a common example:

    Where whichDir is a string containing the directory on which to call ls, sendme is a string into which the full command gets written, and result is the result string.

    Code:
    sprintf (sendme,"ls %s",whichDir);
    fp = popen (sendme,"r");
    fscanf (fp,"%s",result);
    But I mean no harm nor put fault
    On anyone that lives in a vault
    But it's alright, Ma, if I can't please him.

  4. #4
    Join Date
    Aug 2006
    Location
    Belgium
    Beans
    Hidden!
    Distro
    Xubuntu 8.04 Hardy Heron

    Re: parse output of shell command in c++

    or you can have it write output to a named pipe and read from that pipe

  5. #5
    Join Date
    Jun 2006
    Location
    Buenos Aires, Argentina
    Beans
    686
    Distro
    Kubuntu 8.04 Hardy Heron

    Re: parse output of shell command in c++

    Another way is to use system(command), for example: system("ls -a")

  6. #6
    Join Date
    Aug 2005
    Location
    Århus, DenmarK
    Beans
    1,478
    Distro
    Kubuntu 8.10 Intrepid Ibex

    Re: parse output of shell command in c++

    Quote Originally Posted by IYY View Post
    In case you need more detail, here is a common example:

    Where whichDir is a string containing the directory on which to call ls, sendme is a string into which the full command gets written, and result is the result string.

    Code:
    sprintf (sendme,"ls %s",whichDir);
    fp = popen (sendme,"r");
    fscanf (fp,"%s",result);
    How do you define these variables? (I'm a C++ n00b)

  7. #7
    Join Date
    Oct 2007
    Location
    Vienna, Europe
    Beans
    77

    Re: parse output of shell command in c++

    This is C, but I guess it will serve as an example for popen:

    PHP Code:
    #include "stdio.h"

    int main(){
        
    FILEfp;
        
    char result [1000];
        
    fp popen("ls -al .","r");
        
    fread(result,1,sizeof(result),fp);
        
    fclose (fp);
        
    printf("%s",result);
        return 
    0;


  8. #8
    Join Date
    Jul 2005
    Location
    Roma, Firenze - Italy
    Beans
    18

    Re: parse output of shell command in c++

    Should close a stream (file) opened with popen() with:

    pclose(fp);

    just a little thing, i know...

  9. #9
    Join Date
    Sep 2006
    Beans
    2,914

    Re: parse output of shell command in c++

    why do this when you can just do it in C++? See here.

  10. #10
    Join Date
    Jun 2007
    Location
    Porirua, New Zealand
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: parse output of shell command in c++

    Quote Originally Posted by ghostdog74 View Post
    why do this when you can just do it in C++? See here.
    I was a bit baffled at first, but "click click"........

    I think the popen() option is an interesting one as it has application beyond just scanning a directory for files.
    Forum DOs and DON'Ts
    Never assume that information you find using a search engine is up-to-date.
    Please use CODE tags.
    A low-volume blog

Page 1 of 3 123 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •