Difference between File Descriptor and File Pointer
File descriptor is simply an index into the file descriptor table. For each process in our operating system, there is a process control block(PCB). PCB keeps track of the context of the process. So one of the fields within this is an array called file descriptor table.
This array keeps track of all the resources that the process owns and can operate on. The file descriptor table holds pointers to the resources. Resources could be
- Terminal I/O
- Socket for the communication channel between machines
- Any devices
So if any process opens or uses any resources will have an entry in the file descriptor table. Any process when it first starts is given access to three resources. These resources are:
Understand this with an example. Suppose we are working on the terminal then input for it is stdin, the output is stdout and the error is stderr. And if we open any other terminal then both are two different processes now. The newly opened terminal will have its own stdin, stdout because it’s a different process.
The file descriptor is just an integer that you get from the open() system call.
Example of file descriptor:
int fd = open(filePath, mode);
File pointer is a pointer returned by fopen() library function. It is used to identify a file. It is passed to a fread() and fwrite() function.
Example of file pointer:
fp = fopen(“sample.txt,”a”);
fprintf( fp, “Welcome to GFG”);
File pointer provides high efficiency and high portability. It is buffered, unlike the file descriptor. Generally, we use a file pointer when we are reading from a file.
Following is a table of differences between File Descriptor and File Pointer
|1.||File pointer is allocated with fopen function call|
fp = fopen(“sample.txt,”a”);
|File descriptor is allocated with open system call|
int fd = open( filePath, mode );
|2.||It is generally, use for the application which is doing extensive read |
or write from a file
|It is generally used for the application that do frequently random access of file|
|3.||It is a pointer||It is an integer value like 0, 1, 2|
|4.||The file pointer is buffered||The file descriptor is not buffered|
|5.||It is highly portable and efficient||It is less portable and efficient in comparison with the file pointer|
|6.||It is passed to fread() and fwrite() function||It is passed to read() and write() function|
|7.||It is not suitable for inter-process communication||It is suitable for inter-process communication|
|8.||Library functions generally use file pointer||System call generally use the file descriptor|
|9||file pointers are data structures.||File descriptors are represented as integer values|
|10||file pointers are used to represent the current position within the file.||File descriptors are used to identify an open file,|
|11||file pointers are used by the application program to read from or write to the file.||File descriptors are used by the operating system to perform operations on the file,|
|12||file pointers can have any value depending on the position within the file.||File descriptors are non-negative integers,|
Please Login to comment...