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:
“A common and natural result of an undue respect for law is, that you may see a file of soldiers, colonel, captain, corporal, privates, powder-monkeys, and all, marching in admirable order over hill and dale to the wars, against their wills, ay, against their common sense and consciences, which makes it very steep marching indeed, and produces a palpitation of the heart.”
—Henry David Thoreau (18171862)
“We should stop looking to law to provide the final answer.... Law cannot save us from ourselves.... We have to go out and try to accomplish our goals and resolve disagreements by doing what we think is right. That energy and resourcefulness, not millions of legal cubicles, is what was great about America. Let judgment and personal conviction be important again.”
—Philip K. Howard, U.S. lawyer. The Death of Common Sense: How Law Is Suffocating America, pp. 186-87, Random House (1994)
“The professional celebrity, male and female, is the crowning result of the star system of a society that makes a fetish of competition. In America, this system is carried to the point where a man who can knock a small white ball into a series of holes in the ground with more efficiency than anyone else thereby gains social access to the President of the United States.”
—C. Wright Mills (19161962)