Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a stream and a file?

Tags:

c

file

io

stream

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?

like image 583
R__raki__ Avatar asked Jan 05 '14 18:01

R__raki__


People also ask

What are streams and 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.

How does a stream differ from a file Java?

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.

What is file and stream in C++?

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.

How do I convert files to stream?

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.


1 Answers

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 as FILE * 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 type FILE *, 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:

  • Standard Streams: https://linux.die.net/man/3/stdin
  • File Streams: https://linux.die.net/man/3/fopen
  • Pipes: https://linux.die.net/man/3/popen
  • Stream Sockets: https://linux.die.net/man/2/socket

For further reading, also have a look at these links:

  • https://www.gnu.org/software/libc/manual/html_mono/libc.html#I_002fO-Overview
  • https://www.gnu.org/software/libc/manual/html_mono/libc.html#I_002fO-on-Streams

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:

  • https://www.gnu.org/software/libc/manual/html_node/Memory_002dmapped-I_002fO.html
  • https://linux.die.net/man/2/poll
  • https://linux.die.net/man/4/epoll
like image 177
moooeeeep Avatar answered Nov 15 '22 21:11

moooeeeep