Data File - File Pointers and Random Access

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 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. 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:

    Probably nothing in the experience of the rank and file of workers causes more bitterness and envy than the realization which comes sooner or later to many of them that they are “stuck” and can go no further.
    Mary Barnett Gilson (1877–?)

    poor Felix Randal;
    How far from then forethought of, all thy more boisterous years,
    When thou at the random grim forge, powerful amidst peers,
    Didst fettle for the great gray drayhorse his bright and battering
    sandal!
    Gerard Manley Hopkins (1844–1889)

    The nature of women’s oppression is unique: women are oppressed as women, regardless of class or race; some women have access to significant wealth, but that wealth does not signify power; women are to be found everywhere, but own or control no appreciable territory; women live with those who oppress them, sleep with them, have their children—we are tangled, hopelessly it seems, in the gut of the machinery and way of life which is ruinous to us.
    Andrea Dworkin (b. 1946)