The C standard talks about streams. For example the fopen(3)
manual page tells that fopen
is a stream open function.
Can anybody explain what exactly streams are, and how they relate to files?
In the context of the C Standard Library a stream is a generic interface for performing certain I/O operations. You can read from streams, write to streams, some streams are seekable. Opening a file as a stream is only one way to get a stream as an I/O interface for an application.
11.1.1 Streams and File Descriptors
When you want to do input or output to a file, you have a choice of two basic mechanisms for representing the connection between your program and the file: file descriptors and streams. File descriptors are represented as objects of type
int
, while streams are represented asFILE *
objects.File descriptors provide a primitive, low-level interface to input and output operations. Both file descriptors and streams can represent a connection to a device (such as a terminal), or a pipe or socket for communicating with another process, as well as a normal file. [...]
12.1 Streams
For historical reasons, the type of the C data structure that represents a stream is called
FILE
rather than “stream”. Since most of the library functions deal with objects of typeFILE *
, sometimes the term file pointer is also used to mean “stream”. This leads to unfortunate confusion over terminology in many books on C.
Examples for I/O streams in C:
For further reading, also have a look at these links:
The stream-based API is built on top of the low-level file descriptor API and provides additional functionality. Some low-level features are however only available via the lower level API, e.g., memory-mapped I/O, non-blocking I/O or "event-driven" I/O: