Thread: Catching another program's output?

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    6

    Catching another program's output?

    Hi folks

    I've been a developer for almost thirty years now (OMG am I really that old?), but have never really done much in C. Finally now I have a project where I can't avoid it

    The platform is a LinkSys NSLU2 "Slug", running uNSLUng V2.3R63-uNSLUng-6.8 to HDD, with a Velleman K8055 interface board hanging off a USB port, and I'm writing code to interact with the outside world through this board. Running GCC v3.3.5.

    So far, so good. I found a command-line binary for the K8055 which works great - you give it a command an it returns a line of text to the console, telling you what state all the inputs are in. Like this:
    Code:
    $ k8055 -P:2 -D:64
    11;0;134;136;0;0
    Obviously my control code needs to know what the output was. I've solved the problem by sending the output to a file, rather than the stdout console:
    Code:
    $ k8055 -P:2 -D:64 > status.txt
    then opening and reading that file within the code. And this works. It just seems very clunky, not to mention horribly slow.

    Is there a way I can call that external program, and catch its output, without going via a file? Sorta..... route THAT programs stdout to stdin so that I can grab it?

    Just to make life really interesting... the control program (my code) runs as a service, although I'm negotiable on that if it gets in the way.

    I'd also eventually like to re-mash the K8055 code and incorporate it into my own stuff, but right now I can't even compile it (dependancy issues?), and I'll save that one for another day. Right now I'd be happy just to dump the text-file-route I'm currently using.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    6
    In case I wasn't really clear.... this is what the code is currently doing:
    Code:
       sprintf(cmd, "/home/dev/test1/k8055 -P:2 -D:%d > /status.txt", value);
       system(cmd);
       fr = fopen ("/status.txt", "rt");  /* open the file for reading */
       fgets(cmd, 255, fr);
       fclose(fr);
    ( "cmd" is a char[255] )

    so by the end of this lot, the output of the k8055 program is loaded into variable cmd. All I'm looking for here is a more efficient way of doing this.
    Last edited by kenpem; 12-17-2007 at 02:43 PM.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Something like popen() might be what you want.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    6
    Thanks Mac, that might be exactly what I was looking for! Off to play...

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    6
    WAY COOL! popen() is just what we needed. TVM, MacGyver.

    Guess I need to go shopping for a good C reference.... I've put it off long enough!
    Last edited by kenpem; 12-17-2007 at 03:11 PM.

  6. #6
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by kenpem View Post
    WAY COOL! popen() is just what we needed. TVM, MacGyver.

    Guess I need to go shopping for a good C reference.... I've put it off long enough!
    Shopping? Pff, this is the internet. Best C reference there is

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    6

    Thumbs up

    Quote Originally Posted by IceDane View Post
    Shopping? Pff, this is the internet. Best C reference there is
    ...as illustrated by the tip I got about this one.

    But I'm old enough to still get value out of a decent book. And enough of a nerd to read one cover-to-cover to make sure I didn't miss anything important. Like popen().


  8. #8
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by kenpem View Post
    ...as illustrated by the tip I got about this one.

    But I'm old enough to still get value out of a decent book. And enough of a nerd to read one cover-to-cover to make sure I didn't miss anything important. Like popen().

    The man pages(Which you can find online, too) and various user-made references on the internet are quite sufficient in my opinion. But yeah, sometimes having it on hardcopy is just better, I know.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    One of the problems when searching for a function that you don't know the name of is that you don't know what to search for (searching for "catching another program's output" may not get you directly to the right subject, because you'll find lots of references for redirected output to files in the first 1000 or so google hits).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    6

    Smile

    Quote Originally Posted by matsp View Post
    One of the problems when searching for a function that you don't know the name of is that you don't know what to search for (searching for "catching another program's output" may not get you directly to the right subject, because you'll find lots of references for redirected output to files in the first 1000 or so google hits).

    --
    Mats
    Yep, that's exactly why I'm a fan of the cover-to-cover approach. And forums like this, of course! Google was no help at all in helping me solve this problem, other than (eventually) finding the forum.

    Still, all's well that ends well, so let's not start an argument about the value of printed references!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic C input output program help
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 01-27-2008, 06:41 PM
  2. 2d game
    By JordanCason in forum Game Programming
    Replies: 5
    Last Post: 12-08-2007, 10:08 PM
  3. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  4. Trying to store system(command) Output
    By punxworm in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2005, 06:46 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM