I quote below the second edition of The C Programming Language.
B.1 Input and output : <stdio.h>, § 2:
A stream is a source or destinaton of data that may be associated with a disk or other peripheral.
Then one could say that a stream may not be associated to any peripheral : that a stream can exist without being associated to any peripheral.
Therefore the following question come : what look like a stream that is not associated to any peripheral ?
Considering that the authors
B.1 Input and output : <stdio.h>, § 3:
[...] use "file pointer" and "stream" interchangeably [...]
Is
FILE *p;
p = NULL;
such a good answer ?
If "file pointer" and "stream" are indeed used as strict synonyms, then:
FILE *fp = NULL;
defines a stream that is not (yet) associated with any peripheral. Nor is it (yet) associated with any source or destination of data.
If you were to ask Brian Kernighan or the late Denis Ritchie, "Is fp
really a steam there?",
they'd probably say "Well, maybe not. It's a stream whenever it's in use for I/O - when you successfully open it, until you
successfully close it. It's no big deal whether you say it's a stream when it's not in use, or just a file pointer."
A file pointer (or stream) that's in use for I/O is the programmatic representation in C of some
source or destination of data. That source or destination might be - or be on - a peripheral device
such as a USB stick, SSD or HDD, tape volume or terminal. Or it might not. The definition of C
does not specify or restrict the physical implementation of the sources or destinations of data that a FILE *
can represent. That's why the book says that a source or destination of data may be associated with a disk or other peripheral.
The C Standard Library provides only the function fopen
, in <stdio,h>
(and it's fortified variant fopen_s
) as
a way of creating a stream from scratch.1 And it provides the well-known ready-made FILE *
streams stdin
, stdout
and stderr
.
The streams thus provided by <stdio.h>
are normally, but not necessarily, associated with peripheral devices.
C libraries other than the Standard Library may provide you with functions
returning a FILE *
stream that is not associated with a peripheral device. E.g. the
Linux C Library offers you fmemopen
and friends,
which provide FILE *
I/O to memory regions.
You can even read or write FILE *
streams in memory using only the <stdio.h>
facilities. Linux
allows you easily to create a tmpfs
virtual in-memory
filesystem. You may then write programs purely in Standard C that perform regular FILE *
I/O
in your tmpfs
filesystem, and none of it will be associated with a peripheral device.
C does not know or care about the implementation of the filesystem.
freopen
and freopen_s
, which close an existing stream and open
it again with a new filename.