File Pointers and Random Access
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.In C++, random access is achieved by manipulating seekg ( ), seekp ( ), tellg ( ), tellp ( ) functions. The seekg ( ) and tellg ( ) functions are for input streams (ifstream) and seekp ( ) and tellp ( ) functions are for output streams (ofstream). However, if you use them with an fstream object then the above functions return the same value. The most common forms of these functions are :
seekg ( ) - istream & seekg (long) ; Form 1 istream & seekg (long, seek_dir) ; Form 2 seekp ( ) - ofstream & seekp (long) ; Form 1 ofstream & seekp (long, seek_dir) ; Form 2 tellg ( ) - long tellg ( ) tellp ( ) - long tellp ( )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 ; 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 get_pointer (or put_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 current position in the file ios :: end // refers to end of the fileFor 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. fin.seekg(0, ios :: end) ; // go to the end of the file. fin.seekg(-5, ios :: end) ; // back up 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.
Read more about this topic: Data File
Famous quotes containing the words file, random and/or access:
“I have been a soreheaded occupant of a file drawer labeled Science Fiction ... and I would like out, particularly since so many serious critics regularly mistake the drawer for a urinal.”
—Kurt Vonnegut, Jr. (b. 1922)
“Man always made, and still makes, grotesque blunders in selecting and measuring forces, taken at random from the heap, but he never made a mistake in the value he set on the whole, which he symbolized as unity and worshipped as God. To this day, his attitude towards it has never changed, though science can no longer give to force a name.”
—Henry Brooks Adams (18381918)
“Lesbian existence comprises both the breaking of a taboo and the rejection of a compulsory way of life. It is also a direct or indirect attack on the male right of access to women.”
—Adrienne Rich (b. 1929)