C++ File Pointers and Random Access

Every file keeps track of two pointers, which are referred to as get_pointer (in the input mode file) and put_pointer (in the output mode file). These pointers indicate the current position in the file where reading or writing will take place. (A file pointer in this context is not the same as a pointer in C++; rather, it functions in the same way as a book-mark would.) These pointers make it possible to access files in a random order. That means skipping the step where you move through the file in a specific order and going straight to the location you want in the file.

There may be situations where random access is the best choice. For example, if you have to modify a value in record no. 21, then using random access techniques, you can place the file pointer at the beginning of record no. 21 and then straight-way process the record. If sequential access is used, then you'll have to unnecessarily go through the first twenty records in order to reach record 21.

The seekg(), seekp(), tellg(), and tellp() functions

In C++, random access is achieved by manipulating the 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 input streams (ifstream), and the seekp() and tellp() functions are for output streams (ofstream). However, if you use 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. The most common forms of these functions are:

seekg() istream &seekg(long);
istream &seekg(long, seek_dir);
Form 1
Form 2
seekp() ofstream &seekp(long);
ofstream &seekp(long, seek_dir);
Form 1
Form 2
tellg() long tellg()
tellp() long tellp()

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. In the preceding table, seek_dir is defined as an enum seek_dir {beg, cur, end};

When seekg() or seekp() is used according to Form 1, it moves the get_pointer or put_pointer to an absolute position. Here is an 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 the 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. Since seek_dir is an enumeration defined in the header file iostream, it has the following values:

ios::beg        // refers to the beginning of the file
ios::cur        // refers to the current position in the file
ios::end        // refers to the end of the file

Here is an example.

fin.seekg(30, ios::beg);         // Go to byte no. 30 from the beginning of the file linked with "fin."
fin.seekg(-2, ios::cur);         // Back up 2 bytes from the get pointer's current position.
fin.seekg(0, ios::end);          // Go to the end of the file.
fin.seekg(-4, ios::end);         // Backup 4 bytes from the end of the file.

The functions tellg() and tellp() return the position, in terms of a byte number, of put_pointer and get_pointer, respectively, in an output file and an input file.

C++ File Pointers and Random Access Example

Here is an example of a C++ program that uses file pointers and random access to show how they work:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
   string myline;
   ifstream ifs("william.txt", ios::in);

   cout<<"The current position of the file pointer is "<<ifs.tellg();

   cout<<"\n\nThe complete content of the file is:\n";
   while(getline(ifs, myline))
   {
      cout<<myline;
      cout<<"\n";
   }

   cout<<"\nNow the position of the file pointer is "<<ifs.tellg();

   ifs.clear();
   ifs.seekg(2);
   cout<<"\n\nNow the position of the file pointer is "<<ifs.tellg();

   cout<<"\n\nThe content of the file after skipping the first "<<ifs.tellg()<<" characters is:\n";
   while(getline(ifs, myline))
   {
      cout<<myline;
      cout<<"\n";
   }

   ifs.close();
   cout<<endl;

   return 0;
}

Before showing you the output produced by the above program, let me tell you that I already created the "william.txt" file in my current directory (the directory where the C++ source code is saved). The following snapshot is included for your understanding:

C++ file pointer random access example

As you can see, I also wrote two lines of text inside the file "william.txt." Now see the following output produced by the preceding example program illustrating the file pointer and random access in C++.

C++ file pointer random access program

In the above program, you can consider "ifs" as the file pointer for the file "william.txt." and using the following statement:

ifs.clear();

I begin by attempting to clear any fail bits, such as the end-of-file bit, if there are any, before using the following statement:

ifs.seekg(2);

to move the file pointer to the position after the first two characters in the file. I used the "tellg()" method to find the current position of the file pointer "ifs." and used "seekg()" to move the file pointer in the file.

C++ Quiz


« Previous Tutorial Next Tutorial »


Liked this post? Share it!