C++ Reading from another process

fat-tony

[H]ard|Gawd
Joined
Apr 15, 2003
Messages
1,205
I'm trying to capture the output from btdownloadheadless.py, and parse that output using a C++ program. In C, I recall being able to use popen() to do this, however, I can't seem to find something that works with C++ without using C-style reads and writes.

Basically, is there any way to do this without having to kludge together a way to do C-style IO in C++?
 
I might have made that sound more complicated than I meant it to be.

I have my program. I want it to call another program, and be able to read that program's output. The only thing is, I can't just take the entire output and use it, but rather read a chunk, process it, read another chunk, process, repeat.
 
The standard way to do something like this is to redirect the source program's output stream. On Unix systems, you'd want to open some pipes and set the file handle for the child's process's output to the appropriate end of the pipe, then you can read the child's process's stdout from the other end of the pipe. You can do something similar on Windows (I think via a parameter to CreateProcess()).

As an alternative you could use the command line piping syntax
Code:
cmd1 | cmd2 | cmd3
Which will direct stdout from cmd1 to stdin of cmd2 and stdout from cmd2 to stdin of cmd3. Of couse this requires either a run script or user intervention. To see this in action, run
Code:
dir | more
On the Windows command line (of a large directory)

or

Code:
ls | more
On a *nix command line (of a large directory)

dir, ls, and more are each programs, more chops its input into screen-sized chunks and lets the user control screen advancement.
 
Considering that BT is a python program, you'd probably be best off writing your interface in python & just importing the BT module. You'll get a lot more flexibility in controlling BT available to you (since, with the headless interface, once you sdtart it, all you can really do is monitor the output).

What, specifically, is it that you're trying to accomplish here?
 
Well, I'm trying to write up a little gui that lets me queue up a bunch of torrent files, watch their output, and if they either don't go anywhere for a few hours, or if they complete and my share rating gets to a certain point, it kills the downloader, and then gets another one going. I've already considered doing it with cmd1 | cmd2 | cmd3 etc... but that won't work for this situation, because the program needs to spawn the client, and then monitor its output.

And it's also a matter of curiousity. I'd love to know if there's a native way of doing this in C++. As I said before, I know that popen from C would do it, but then I'd have to work with C-style filehandles, and that sucks compared to using all the safer C++ stuff.
 
Well, short of just monitoring things, you can't really do much unless you decide to embed the Python inside your C++ app. You'd probably be better off either rewriting the protocol handler in C++ yourself or just picking up Python & learning enough to write your GUI in it.


Being a fairly avid torrenter myself, it sounds like you're wanting to write something like Azureus. Other than being written in Java (if you check it out, listen to them when they say you need to have a certain JRE version or higher), it's a nice program.
 
Originally posted by ameoba
Well, short of just monitoring things, you can't really do much [...]

That's all I want to do :). I want my code to be able to spawn a process, read from it, and output the results. However, it has to be my program that loads the other program, so shell redirection won't work.
 
Back
Top