This question is not fully related to the programming language. Although the library does affect what happens when the file is opened (for example, using open or fopen ), the main behavior comes from the operating system.
Linux, and I assume that other OSs in most cases read ahead. This means that the file is actually read from the physical storage even before you call read on the file. This is done as an optimization, reducing read time when the file is actually read by the user. This behavior can be partially controlled by the programmer using a special flag for open functions. For example, the Win32 API CreateFile can specify FILE_FLAG_RANDOM_ACCESS or FILE_FLAG_SEQUENTIAL_SCAN to indicate random access (in this case the file is not read in advance) or sequential access (in this case, the OS will perform rather aggressive forward reads), respectively. Other OS APIs may give more or less control.
For the basic ANSI C API open , read , write , which use a file descriptor, the file descriptor is a simple integer that is passed to the OS and designates the file. In the OS itself, this most often translates into some structure that contains all the necessary information for the file (name, path, search for offsets, size, reading and writing buffers, etc.). The OS will open a file, which means searching for a specific entry in the file system ( inode for Linux), which matches the path specified in the open method, creates a file structure and returns an identifier to the user - a file descriptor. From this moment, the OS freely reads any data that seems to be suitable, even if it is not requested by the user (often reading more requests is required in order to at least work in the proper size of the file system).
Eli iser
source share