问题
I'm reading a section in 'C Primer Plus' which deals with files, streams and keyboard input. The author connects the concept of stream with files and defines stream as follows:
Conceptually, the C program deals with a stream instead of directly with a file. A stream is an idealized flow of data to which the actual input or output is mapped. That means various kinds of input with differing properties are represented by streams with more uniform properties. The process of opening a file then becomes one of associating a stream with the file, and reading and writing take place via the stream
What does the author mean by the bold sentence? And what is the connection between files and stream?
回答1:
The people designing C wanted a uniform way of interfacing with different sources of sequential data, like files, sockets, keyboards, USB ports, printers or whatever.
So they designed one interface that could be applied to all of them. This interface uses properties that are common to all of them.
To make it easier to talk about the things that could be used through the interface they gave the things a generic name, streams.
The beauty of using the same interface is that the same code can be used to read from a file as from the keyboard or a socket.
回答2:
I struggled with the same question when first came across it. :) But please note that files and streams are quite different things. Files are just sequences of bytes. Now on the other hand, any program (most of them) will normally need to interact with their environment(could be files, could be devices, could be network, etc.) so the stream comes in here nicely.
A stream is an interface(an easy "face" to work with something with many subtleties irrelevant to us, just like a TV remote!) for triggering input/output flow of data, to or from anything that can be a destination or source to that input/output data, hiding the implementation details of the numerous possibilities of OS methodologies and hardware designs from programmers(you as a programmer are not really interested into knowing about how various operating systems deal with files on low level, or how they interact with a network socket, or with a monitor, and so on, right?).
So for instance, if you want to read/write to file, you first need a stream. You create a stream for that file (in C) using the fopen()
function from the standard input/output library. The thing that is returned is of course a pointer to something called FILE but that’s a traditional “bad choice of word” for stream(https://www.gnu.org/software/libc/manual/html_node/Streams.html), so yes, FILE type in C is indeed the data structure representing a stream!(I see..., crazy!)
Also, as another example, the way your program can get input from keyboard, how does that happen? That happens through a hidden(automatically made) stream the OS makes for ANY program as soon as they get run and again, standard input/output library of C, gives you access to that through a pointer called “stdout”(stdout is a pointer to the output stream, a stream automatically made by the OS for your program when you run it).
Also as an example in C++ world, you know that in there things are in classes and objects instead of structures, so there you will encounter (as an example) an object called "cout" which is indeed an output stream "object". It will be triggered to show something on the monitor(by default) if you use its "<<" method(AKA an overloaded operator). You see, you didn't need to write any more code for sending something from your program to the monitor! You know cout is already prepared and connected to that! That's awesome, isn't it?
Here is also a pretty decent explanation if you need to read further: http://www.qnx.com/developers/docs/6.5.0/topic/com.qnx.doc.dinkum_en_c99/lib_file.html
HTH.
回答3:
A stream is the C way
of dealing with different data mediums/sources. These
can include say
- A file
- A socket
and so on. A stream
, as an interface, helps you forget how data is managed under the hood and concentrate on desired objectives.
回答4:
A stream is a logical entity that represents a file or device, that can accept input or output. All input and output functions in standard C, operate on data streams. Streams can be divided into text, streams and binary streams.
来源:https://stackoverflow.com/questions/38652953/what-does-stream-mean-in-c