Introduction
Every file maintains two pointers called get_pointer (in input mode file) and put_pointer (in output mode file) which tell the current position in the file where writing or reading will take place. These pointers help attain random access in 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 you have to modify a value in record no. 21 and then using a random access techniques, you can place the file pointer at the beginning of record 21 and then straightway process the record. If sequential access is used, then you'll have to unnecessarily go through first twenty records in order to reach at record 21.
Functions used for random access
In C++, random access is achieved by manipulating seekg(), seekp(), tellg() and tellp() functions.
The seekg() and tellg() functions allow you 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 for output streams (ofstream). However, if youuse them with an fstream object then tellg() and tellp() return the same value. Also seekg() and seekp() work the same way in an fstream object.
.png) |
Forms of pointer functions |
Using functions for random access
The working of seekg() & seekp() and tellg() & tellp() is just the same except that seekg() and tellg() work for ifstream objects and seekp() and tellp() work for ofstream objects. Let us now understand their working.
The seekg() (or seekp()) when used according to Form 1, it moves the get_pointer (or put_pointer ) to an absolute position.
For example,
ifstream fin;
ofstream fout;
: // file opening routine
fin.seekg(30) will move the get_pointer (in ifstream) to byte number 30 in the file.
fout.seekp(30) will move the put_pointer (in ofstream) to byte number 30 in the file.
When seekg() (or seekp()) function is used according to Form 2, it moves the pointer to a position relative to the current position, following the definition of seek_dir.
Seek_dir is an enumeration (defined in iostream.h) that has following values.
ios :: beg, // refers to beginning of the file
ios :: cur, // refers to the current position in the file
ios :: end // refers to end of the file
For example,
fin.seekg(30, ios :: beg ); // go to byte no. 30 from beginning of the file linked with fin.
fin.seekg(-2, ios :: cur); // back up 2 bytes from current position of get pointer
fin.seekg(0, ios :: end); // go to the end of the file
fin.seekg(-5, ios :: end); // backup 5 bytes from end of the file
The methods tellp() and tellg() return the position (in terms of byte number) of put_pointer and get_pointer respectively in an output file and input file respectively.
In the following section we shall put all these concepts into practice when we learn to implement basic operations on files.
Recourses:-
1. Computer science with C++ Class XII , Sumita Arora ( Dhanpat Rai & Co. (P) Ltd.)
Comments
Post a Comment