Streaming data between programs

Started by
5 comments, last by GameDev.net 19 years, 2 months ago
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?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Advertisement
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.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
It also sounds to me that this would be a good question to bring up with your instructor.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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,
[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
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
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

This topic is closed to new replies.

Advertisement