Get stream output from a program

I'd like to be able to get output from a program the way some (most) IDEs can get the error messages that compilers like GCC output. I can't think of any way to do this.

As an example,
a.cpp:
1
2
3
4
5
6
#include <iostream>

int main() {
    std::cout << "Hello, world!\n";
    return 0;
}


b.cpp
1
2
3
4
5
6
7
#include <iostream>

int main() {
    std::cout << OutputFromA << "\n";
    std::cin.get();
    return 0;
}


In the example, b should output "Hello, world!\n" too. How can I do this?

Finally, does it make it harder if a is a Python script?

Thanks.

By the way, I realise it's not going to be anywhere near as simple as that (although I can't imagine it's incredibly difficult).

I've thought about writing the text to a file, then opening it with the C++ program and printing the contents, then closing the file and deleting it. I guess that would work... but it means alot of modifying...
Last edited on
closed account (z05DSL3A)
pipes?
Oh? So I create a child process with fork() and then pipe it's output to the parent process?

I didn't think of that. Thanks.
Standard way is

1. Create pipe.
2. fork().
3. In child process:
a. close stdout and stderr
b. dup2 the pipe to stdout and stderr
c. close read end of pipe
d. exec the new program
4. In parent process:
a. close write end of pipe
b. read from the read end of pipe.

Ok, thanks. I'll do that.
Topic archived. No new replies allowed.