What does 'stream' mean in C?
Asked Answered
D

6

21

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?

Dissonance answered 29/7, 2016 at 7:28 Comment(5)
Search and read about file streams in C from various other sources on internet viz. tutorialspoint.com, etc. you will understand it.Lugo
You have already marked the answer in bold letters. That is exactly what it is. Not a bad question afterall, but i doubt if it belongs hereSloop
Maybe you can imagine it like a pipe (a tube which conveys water) - a connection through which data can flow in and out.Mcatee
@BatCoder What do you mean by pipe?Dissonance
@Don'tYouWorryChild tutorialspoint is the last place you should be reading about C or anything programming related.Cocteau
F
31

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

Francesco answered 22/10, 2018 at 19:13 Comment(2)
@M-S you mentioned input from keyboard, how does that happen? with explicitly stdout . why is out? you take input. shouldn't it be stdin ?Codex
@JinLim Sure that was a mistake, thanks for mentioning it, I've now fixed it in an edit.Francesco
R
25

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.

Rye answered 29/7, 2016 at 7:47 Comment(3)
Is a stream a file itself in its essence? (just out of curiosity)Santanasantayana
What IS a file? A file is an abstraction. It's an adress somewhere in memory that indicates the first byte in a series of bytes (it can also be fragmented if it's a large file). A stream is also a memory location where bytes are written and bytes are read. In that sense, it is like a file in that you can write and read from it. But they're both abstractions. They're simply memory offsets where one can read and write dataSherylsheryle
Is stream an operating system level or C language level concept ?Gherkin
L
5

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.

Lefevre answered 8/9, 2016 at 12:45 Comment(0)
C
1

Simply a stream is a FILE * Like function C FILE * fopen ( const char * filename, const char * mode ) This function return a File *

FILE:

Object containing information to control a stream Object type that identifies a stream and contains the information needed to control it, including a pointer to its buffer, its position indicator and all its state indicators.

Cutty answered 21/5, 2020 at 18:0 Comment(0)
S
0

A stream is the C way of dealing with different data mediums/sources. These can include say

  1. A file
  2. 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.

Sloop answered 29/7, 2016 at 7:48 Comment(0)
E
0

My C programming degree uses this is an explanation if it helps: "A Stream is an abstraction of a file that provides a consistent interface to the programmer, regardless of the actual device."

Eboni answered 9/5, 2020 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.