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?
A file can be: A data set that you can read and write repeatedly. A stream of bytes generated by a program (such as a pipeline). A stream of bytes received from or sent to a peripheral device.
File descriptors are represented as objects of type int , while streams are represented as FILE * objects. File descriptors provide a primitive, low-level interface to input and output operations.
C++ Files and StreamsIt is used to create files, write information to files, and read information from files. ifstream. It is used to read information from files. ofstream. It is used to create files and write information to the files.
First create FileStream to open a file for reading. Then call FileStream. Read in a loop until the whole file is read. Finally close the stream.
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.
Let me quote:
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. [...]
... and further:
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With