Thread: Calling external program and reading its output.

  1. #1
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003

    Question Calling external program and reading its output.

    I have a program that gathers data from a machine and outputs it to the command line from which it is called. I would like to write a program that calls the other program, reads its output, and writes that to a file. Here's an example of the output from the program:

    Polling slave ...
    [123]: 112
    [124]: 101
    [125]: 96
    [126]: 101
    [127]: 187
    [128]: 194
    [129]: 171
    [130]: 177
    [131]: -80
    [132]: 0
    [133]: 0
    [134]: 69
    [135]: 0
    [136]: 1016
    [137]: 0
    [138]: 2555
    [139]: 0
    [140]: 2437
    [141]: 108

    So I know I can use system() to call the program, but that only returns an integer when I need all that garbage above. Could someone point me in the right direction?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Just redirect the output of that program into a file.
    program > outputfile
    This can be done from within system(), or even from the command line. It's reasonably portable, as well, though it assumes that you have write permissions.

    Alternatively, and less portably but more conveniently, you could use popen(), if you wanted to process that data afterwards.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    popen() doesn't exist in Windows (from MS CRT at least), but fortunatley there is a replacement called _popen() that apparently does similar enough a functionality to be perfect replacement. Using something like
    #define popen _popen
    should make it work.

    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.

  4. #4
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    I used the
    program > outputFile
    suggestion and it worked just fine. Thanks all!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. calling an external program + capture output?
    By cyberfish in forum C++ Programming
    Replies: 4
    Last Post: 03-21-2008, 12:49 AM
  4. Replies: 2
    Last Post: 01-28-2008, 03:07 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM