Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'stream' mean in C?

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?

like image 508
Jin Avatar asked Jul 29 '16 07:07

Jin


People also ask

What is a stream in programming?

STREAMS is a general, flexible programming model for UNIX system communication services. STREAMS defines standard interfaces for character input/output (I/O) within the kernel, and between the kernel and the rest of the UNIX system. The mechanism consists of a set of system calls, kernel resources, and kernel routines.

What are types of streams in C?

There are three types of streams in C or any Programming language, Buffered, Line-buffered and Unbuffered.

What is the difference between file and stream in C?

The file descriptor interface provides only simple functions for transferring blocks of characters, but the stream interface also provides powerful formatted input and output functions ( printf and scanf ) as well as functions for character- and line-oriented input and output.


2 Answers

Notice that files and streams are quite different things. Files are just sequences of bytes, while Streams are just facilitators(helpers).

Streams come in the picture since all programs need to interact with their surrounding environment in many different forms(could be files, could be I/O devices such as monitor and keyboard, could be network sockets and etc.).

So a stream is an interface(an easy "face" to work with something that has many subtleties irrelevant to us, just like we don't need to know how a TV remote works!) for triggering input/output flow of data, from/to anything that can be a source/destination to that input/output data, hiding the low-level implementation details of the numerous methodologies that OSs devise in order to interact with the variously designed hardware, on behalf of the programmers(i.e., We -as programmers- are not really interested in re-programming the way an operating system interacts with various hardware every time we create new software).

So for instance, thinking about the way our program can get input from the keyboard..., how does that happen? That happens through a hidden(hidden to the programmer) stream that the OS provides for every "process"(As soon as a program is run, it will be what's called a process), and the OS gives the address to the standard stream made for a process to it automatically(i.e., we won't need to write code to locate its address). This stream is commonly called the "stdin"(rooted in the C & Unix terminology), or more formally called "The Standard Input Stream". Our programs, no matter written in what language, must be able to use such standard streams made by the OS through the standard I/O libraries of that language. As an example, in the C programming language, we may scan the standard input stream by calling the function "scanf"(scanf will know where the stdin of our program is automatically).

But as another important example, again in C, let's say this time our program wants to write user's input to a "file"... Does only the existence of the stdin stream suffice in this situation? Of course not! This time, we'll need to use a pair of streams, one already provided by the OS, the stdin, to get the user's input, and a second one, to let the communication between our program and the file! So we will have to create this second stream! Something which can be done by calling the fopen() function. (Fun Fact: In the manual, if you notice, you will see that the returned type of this function is a pointer to a structure called FILE, but that’s only a traditional “bad choice of word” for what's actually a pointer to a "stream"! Yes, the type FILE in C is indeed a stream, and not a file!(I see, crazy!) So remember, the pointer FILE* does NOT point to the actual file, it points to a stream containing the information about that file, including information about the buffer used for the file's I/O and etc.)

Notice: The streams we create ourselves(for instance file streams) can be bidirectional, while standard streams are unidirectional. This is also illustrated nicely with arrows in the picture below: enter image description here

Also as an example in the C++ world to give you a comparison, you know that in there, things are in classes instead of structures, so you will encounter an object called "cout"(the output stream object) if you're outputting, which is an object connected to the output stream(stdout in C), and is an instance of the class ostream(from the class hierarchy ios_base <-- ios <-- ostream). To write to the standard output stream using cout, its "<<" method(corresponding to printf() in C) must be used. This time again, cout will not suffice for interacting with other things(such as files) and we'll need to create our own streams. In C++, it can be done by instantiating the ifstream and ofstream classes(corresponding to FILE structure in C) which will result in objects that basically play the same role as the pointer "FILE*" in C.

Hope That Helps.


illustration's credit to linuxhint.com

like image 163
M-J Avatar answered Oct 05 '22 04:10

M-J


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.

like image 36
Klas Lindbäck Avatar answered Oct 05 '22 04:10

Klas Lindbäck