Sign in to follow this  
Endar

Streaming data between programs

Recommended Posts

Endar    668
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 this post


Link to post
Share on other sites
LessBread    1415
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 this post


Link to post
Share on other sites
Endar    668
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 this post


Link to post
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 this post


Link to post
Share on other sites
JohnBSmall    881
[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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this