system call output

Discussion in 'Mac Programming' started by farmerdoug, Sep 21, 2011.

  1. farmerdoug macrumors 6502a

    Joined:
    Sep 16, 2008
    #1
    If I use a system call
    system("wc -l ts.txt");
    do I have to direct the output to a file
    system("wc -l ts.txt > lines.txt");
    and then read the file
    or is there away to direct it directly to a variable.

    thanks
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    See the man page for the popen() library function.

    If that doesn't meet your needs, or you can't work out how to use it, then the answer to your question is "No": there is no way in C to direct the output of a sub-command directly to a variable*.


    * other than writing such a function yourself, using existing functions such as system() or popen().
     
  3. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #3
    Yes, I think you told me about popen awhile ago. Forgot about it.
    Thanks.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    The man page for system() has a SEE ALSO entry for popen().

    Often, an easy way to connect the dots is to read the man page for the first dot, and see where it points you.

    For Xcode < 4, there's an "Open man page..." item under the Help menu. The page is presented as HTML, with clickable links for SEE ALSO entries.

    For Xcode >= 4 I don't know.

    And there's this tool:
    http://www.bruji.com/bwana/
     
  5. farmerdoug thread starter macrumors 6502a

    Joined:
    Sep 16, 2008
    #5
    I looked at the help page but didn't follow through on the see also's. Popen did register.

    corncob = popen("wc -l ts.txt", "r");
    fgets(buf,100,corncob);
    pclose(corncob);
    sscanf(buf,"%d %s", &no_lines, junk);
    printf("%d\n", no_lines);
     

Share This Page