What is file pointers ?

File pointers :

A file pointer is a pointer that is used to handle and keep track of the files being accessed.

The file pointer is declared as FILE *fp. Where ‘fp’ is a file pointer. Every file maintains two pointers called get_pointer (in input mode file) and put_pointer (in output mode file) which tells the current position in the file where reading or writing will take place. These pointers help to attain random access in the file. That means moving directly to any location in the file instead of moving through it sequentially.

There may be situations where random access is the best choice. For example, if required to modify a value in record no 21, then using random access techniques, it can place the file pointer at the beginning of record 21 and then straight-way process the record. If sequential access is used, then will  have to unnecessarily go through the first twenty records to reach a record of 21

In C++, random access is achieved by manipulating seekg(), seekp(), tellg() and tellp() functions. The seekg() and tellg() functions allow  to set and examine the get_pointer, and the seekp() and tellp() functions perform these operations on the put_pointer.

The seekg() and tellg() functions are used for input streams (ifstream) and seekp() and tellp() functions are used for output streams (ofstream). However, if use them with a stream object then tellg() and tellp() return the same value.

Post a Comment

Thankyou

Previous Post Next Post