What are these standard streams and how do they work? Are they just like normal files where one program writes to the file and another reads from it? If so, how can two programs access the same file at once?

8 Years
Discussion Span
Last Post by Rashakil Fol

> If so, how can two programs access the same file at once?
I thought we were talking about streams, not files.

A stream is a conduit between a producer (say your keyboard), and a consumer (your program).

A stream can be redirected, so instead of the producer being a keyboard, it can be a function reading from a file, and stuffing those characters onto the writing end of the stream.

Votes + Comments
Nice explanation.

Thanks for the reply.. but could someone explain this in a little more detail?


Try this: http://www.cplusplus.com/reference/clibrary/cstdio/

I'm afraid that link didn't help much.. I'm having trouble understanding how a stream connects to other programs or devices. For example, most tutorials just say the output stream connects to the monitor and the input stream connects to the keyboard. How is this connection achieved?

Edited by mahela007: n/a


How is this connection achieved?

Depends on the implementation.

Input and Output operations can also be performed in C++ using the C Standard Input and Output Library (cstdio, known as stdio.h in the C language). This library uses what are called streams to operate with physical devices such as keyboards, printers, terminals or with any other type of files supported by the system.
Streams are an abstraction to interact with these in an uniform way; All streams have similar properties independently of the individual characteristics of the physical media they are associated with.

(source: http://www.cplusplus.com/reference/clibrary/cstdio/)


How is this connection achieved?

On Unix systems, when you launch a program from a shell, the shell forks off a process and the process inherits the shell's open standard input and standard output streams. When you run a command like ./foo > outfile , the shell forks, opens the file for writing, moves its file descriptor to replace the standard output stream, and then executes foo. When you run a chain of commands separated by pipes, the shell creates the pipes and forks off the processes, in some order.

From the process's perspective, they're just (on Unix systems) the file descriptors numbered 0 and 1. Those file descriptors point to the standard input and standard output streams (I forget which is which, though). The file descriptors might, for example, be pipes that are listened to by a terminal emulator, which renders the data on the screen, or they might be channeling data to some other program launched on the same command line, or they might be file descriptors for open files. You can in fact, at least on Unix systems, alter what your standard input and standard output streams point to, in the middle of your program.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.