In C when we open a file what happens?? As I know that the contents of the file is not loaded in the memory when we open a file. It just sets the file descriptor ? So what is this file descriptor then?? And if the contents of the file is not loaded in the memory then how a file is opened?
Function fopen() is used both to open or create a file, and fclose() is used to close an already opened file. File 'read' and 'write' operations can be performed after opening a file. The fputc() and fputs() functions are used to write characters and strings respectively in a file.
The open function creates and returns a new file descriptor for the file named by filename . Initially, the file position indicator for the file is at the beginning of the file.
A File is a collection of data stored in the secondary memory. So far data was entered into the programs through the keyboard. So Files are used for storing information that can be processed by the programs. Files are not only used for storing the data, programs are also stored in files.
Typically, if you're opening a file with fopen
or (on a POSIX system) open
, the function, if successful, will "open the file" - it merely gives you a value (a FILE *
or an int
) to use in future calls to a read function.
Under the hood, the operating system might read some or all of the file in, it might not. You have to call some function to request data to be read anyways, and if it hasn't done it by the time you call fread
/fgets
/read
/etc... then it will at that point.
A "file descriptor" typically refers to the integer returned by open
in POSIX systems. It is used to identify an open file. If you get a value 3
, somewhere, the operating system is keeping track that 3
refers to /home/user/dir/file.txt
, or whatever. It's a short little value to indicate to the OS which file to read from. When you call open
, and open say, foo.txt
, the OS says, "ok, file open, calling it 3
from here on".
This question is not entirely related to the programming language. Although the library does have an impact on what happens when opening a file (using open
or fopen
, for example), the main behavior comes from the operating system.
Linux, and I assume other OSs perform read ahead in most cases. This means that the file is actually read from the physical storage even before you call read
for the file. This is done as an optimization, reducing the time for the read when the file is actually read by the user. This behavior can be controlled partially by the programmer, using specific flag for the open functions. For example, the Win32 API CreateFile
can specify FILE_FLAG_RANDOM_ACCESS
or FILE_FLAG_SEQUENTIAL_SCAN
to specify random access (in which case the file is not read ahead) or sequential access (in which case the OS will perform quite aggressive read ahead), respectively. Other OS APIs might give more or less control.
For the basic ANSI C API of open
, read
, write
that use a file descriptor, the file descriptor is a simple integer that is passed onto the OS and signifies the file. In the OS itself this is most often translated to some structure that contains all the needed information for the file (name, path, seek offsets, size, read and write buffers, etc.). The OS will open the file - meaning find the specific file system entry (an inode
under Linux) that correlates to the path you've given in the open
method, creates the file structure and return an ID to the user - the file descriptor. From that point on the OS is free to read whatever data it seems fit, even if not requested by the user (reading more than was requested is often done, to at least work in the file system native size).
C has no primitives for file I/O, it all depends on what operating system and what libraries you are using.
File descriptors are just abstracts. Everything is done on the operating system.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With