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?
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?
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.
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.