# Streaming data between programs

## Recommended Posts

I was told in class that, using C++ streams, you can open and stream data to other running programs, like you can stream to and from files. I'm not doing anything specific, just, right now it interests me. So, how would this be done? A special type of stream? How would you connect them up? Would they stop and wait for data input, like getchar() does?

##### Share on other sites
It sounds to me like your instructor was talking about piping between programs. A 'pipe' is a command line argument that instructs the operating system to use the output from one program as the input to another program. Here's a quick overview - CS330 Pipes and Filters. Similar things can be done on Windows machines as well. Having two programs exchange data between themselves is known as interprocess communication. The mechanisms are specific to the operating system that the programs run on.

##### Share on other sites
I know about piping and interprocess communication. I was pretty sure that he said stream because this was my C++ class.

Maybe I misheard. Anyway, if I didn't and anyone else has heard about anything else like this.... :D

##### Share on other sites
It also sounds to me that this would be a good question to bring up with your instructor.

##### Share on other sites
Well, in theory, nothing prevent you to write a stream which will use pipes (or any other interprocess communication technique) to handle this. AFAIK there is no such thing in the stl, and it seems that it will not be added in the C++ 0x standard (or did I miss it? it seems it is not even part of the official extended stl wishlist).

Regards,

##### Share on other sites
[someone please correct me if I'm wrong here... this is all afaik]

Piping can occur in a couple of ways. Either the pipe can be set up by the command shell, in which case it's totally transparent to your program, or you can use (operating system dependant) functions to open a pipe within your program.

If you've got a command prompt up, and type something like:
> foo | bar

Then the command shell will start both programs. The standard output stream from 'foo' (std::cout in C++) will be piped straight through to the standard input stream in 'bar' (std::cin). 'foo' and 'bar' don't know or care that their output/input is actually going to/coming from another process, not the user, or a file.

Pipes can also be opened from within a program, in which case the program opening the pipe will start the other program, and get access to its standard input, output and error streams (through a standard C FILE pointer, or through an OS specific file handle).

Piping and streams are related though - a pipe is the connection between two programs, but from each program's point of view, it's a stream (an input stream on one side and output stream on the other).

Not necessarily a standard C++ stream type though.

John B

##### Share on other sites
If you want to, you can extend the streambuf class to handle pipes. I extended it once to handle sockets. This way, you can have iostreams for any inputs you want! Details: See part 5 here: http://www.horstmann.com/cpp/iostreams.html

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628367
• Total Posts
2982279

• 10
• 9
• 13
• 24
• 11