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:

    While waiting to get married, several forms of employment were acceptable. Teaching kindergarten was for those girls who stayed in school four years. The rest were secretaries, typists, file clerks, or receptionists in insurance firms or banks, preferably those owned or run by the family, but respectable enough if the boss was an upstanding Christian member of the community.
    Barbara Howar (b. 1934)

    Novels as dull as dishwater, with the grease of random sentiments floating on top.
    Italo Calvino (1923–1985)

    Oh, the holiness of always being the injured party. The historically oppressed can find not only sanctity but safety in the state of victimization. When access to a better life has been denied often enough, and successfully enough, one can use the rejection as an excuse to cease all efforts. After all, one reckons, “they” don’t want me, “they” accept their own mediocrity and refuse my best, “they” don’t deserve me.
    Maya Angelou (b. 1928)