Reputation: 1161
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?
Upvotes: 7
Views: 974
Reputation:
When you open the file then the file pointer gets the base address(starting address)of that file.Then you use different functions to work on the file. EDIT: Thanks to Chris,here is the structure which is named FILE
typedef struct {
int level; /* fill/empty level of buffer */
unsigned flags; /* File status flags */
char fd; /* File descriptor */
unsigned char hold; /* Ungetc char if no buffer */
int bsize; /* Buffer size */
unsigned char *buffer; /* Data transfer buffer */
unsigned char *curp; /* Current active pointer */
unsigned istemp; /* Temporary file indicator */
short token; /* Used for validity checking */
} FILE;
Upvotes: 0
Reputation: 2834
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).
Upvotes: 4
Reputation: 146053
If the program uses fopen()
then a buffering package will use an implementation-specific system call to get a file descriptor and it will store it in a FILE
structure.
The system call (at least on Unix, Linux, and the Mac) will look around on (usually) a disk-based filesystem to find the file. It creates data structures in the kernel memory that collects the information needed to read or write the file.
It also creates a table for each process that links to the other kernel data structures necessary to access the file. The index into this table is a (usually) small number. This is the file descriptor that is returned from the system call to the user process, and then stored in the FILE struct.
Upvotes: 1
Reputation: 44256
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".
Upvotes: 7
Reputation: 16577
As already mentioned it is OS functionality.
But for C file I/O most probably you need info on fopen
function.
If you will check description for that function, it says :
Description:
Opens a stream.
fopen opens the file named by filename and associates a stream with it. fopen returns a pointer to be used to identify the stream in subsequent operations.
So on successful completion fopen
just returns a pointer to the newly opened stream. And it returns NULL in case of any error.
Upvotes: 0
Reputation: 1695
File descriptors are just abstracts. Everything is done on the operating system.
Upvotes: 1
Reputation: 1788
C has no primitives for file I/O, it all depends on what operating system and what libraries you are using.
Upvotes: 2