File stream

From LQWiki
Jump to navigation Jump to search

Since files exist on disk partitions and disks are hardware, the business of transferring data to and from files has to be carried out by the Linux kernel. The kernel identifies each open file by a number called a file descriptor.

Programs written in C or C++ wrap this descriptor in a structure called a stream or FILE pointer. The FILE structure includes a field for the descriptor and also other useful fields such as input or output buffers, a bookmark, various flags and error markers such as EOF. When a file is opened using the high-level C function fopen(), a FILE structure is created and a pointer to it returned. This pointer can then be used as an identifier for the file in various read and write functions.