Home > database >  Why does C use FILE for stdin/stdout streams as well as file operations?
Why does C use FILE for stdin/stdout streams as well as file operations?

Time:09-26

I have recently looked into files and input/output in C and quickly figured out that C uses a typedefed structure known as FILE which is commonly represented as a pointer to a structure.
This structure contains data about input and output so lower level parts of the operating system and kernel can easily manage input and output on a system, for example the size of the data being inputted, the type of data etc.
Basically is it purposefully made abstract since the way it works is very low level.

I also know that something like FILE* x is commonly used by C programmers to represent input and output streams. C also allows FILE to commonly be used to point to a file.

Why does C allow programmers to use FILE to represent files as well as input/output streams? To me this sounds very confusing as well as this the keyword FILE is literally called FILE and i would think it would often confuse new programmers into thinking it is used to ONLY point to files.

So why is FILE used for both files and input/output?

CodePudding user response:

Despite its name, FILE has nothing to do with files. For example, you can't rename using a FILE. You can't delete using a FILE.

FILE always represents a stream. It's of no relevance whether this stream is associated with a plain file, a pipe or a socket.

CodePudding user response:

Because FILE is misnamed, it doesn't represent files. It represents something that can be read from and written to. That reading from and writing to can be done in single character sizes (streams) or bulk (blocks).

So if you want keyboard input, why not read from the keyboard buffer? If you want to send a web request, why not write one to the outbound network buffer? What would you use to do that?

Once the designers of UNIX and C realized that the core common feature set of many items was the ability to read and write, they reused the pattern for many things that people don't consider files. This is the motivation behind the UNIX "everything is a file" mantra. Directories are files, containing the names and locations of other files. Disks are files, expecting writes that follow filesystem layout constraints. Screens are files, expecting terminal control codes and text.

And what happened to files, the start of this powerful idea? They're still there, but the FILE construct in C is more of an ID in the kernel for a thing that can be possibly read or written from, and isn't guaranteed to back to the regular notion of a file on disk.

CodePudding user response:

On Linux systems, you can study the source code of GNU glibc. It is open source code.

a FILE is then defined as a pointer to some struct containing an open file descriptor number, some buffer, etc...

The short name FILE is historical. Invented at a time where a Unix computer had less than 256 kilobytes of memory, and rotating hard disks of a few megabytes. Read wikipedia on History of Unix.

CodePudding user response:

In many systems (like Linux) (amost) everything is a file. Terminal, network card, random number generator, device tree and much more. Stream (or file) is a very good abstraction for almost anything :)

CodePudding user response:

In C, programmers can create, open, close text or binary files for their data storage.

A file represents a sequence of bytes, regardless of it being a text file or a binary file. A c programming language provides access on high-level functions as well as low-level (OS level) calls to handle files on your storage devices.

  • Related