|
File Pointers
The C++ input and output system
manages two integer values associates
with a file.
These are:
• get pointer – specifies the location
in a file where the next read
operation will occur.
• put pointer – specifies the location
in a file where the next write
operation will occur.
In other words, these pointers
indicate the current positions for
read and write operations,
respectively. Each time an input or an
output operation takes place, the
pointers are automatically advances
sequentially.
The term pointers should not be
confused with normal C++ pointers used
as address variables.
Often you may want to start reading an
existing file from the beginning and
continue sequentially until the end.
When writing, you ma want to start
from the beginning, deleting any
existing contents, or appending new
records (in which case you can open
the file with the ios:app mode
specifier). These are default actions,
so no manipulation of the pointers is
necessary.
Sometimes you may have to manipulate
file pointers to read from and write
to a particular location in a file.
The seekg() and tellg() functions
allow you to set and examine the get
pointer, and the seekp() and tellp()
functions perform these same actions
on the put pointer. In other words,
these four functions allow you to
access the file in a non-sequential or
random mode.
All the iostream clas objects can be
repositioned by using either the seekg()
or the seekp() member function. These
functions move the get and put
pointers respectively to an absolute
address within the file or toa certain
number of bytes from a particular
position.
The tellg() and tellp() functions can
be used to find out the current
position of the get and put file
pointers respectively in a file.
The seekg() member function takes two
arguments:
• Number of bytes to move.
• Reference in the file from which the
pointer has to be repositioned.
For example:
If stream iFi1;
iFi1.seekg(10,ios:beg);
means, “position the get pointer 10
bytes from the beginning of the file”
The first argument is an integer that
specifies the number of bytes
positions(also called offset). The
second argument is the reference
point. There are three reference
points defined in the ios class:
• ios:beg – the beginning of the file.
• ios:cur – the current position of
the file pointer
• ios:end – the end of the file
A negative value can also be specified
for the first argument. For example,
the following statement moves the file
pointer 20 bytes backward from the end
of the file.
iFi1.seekg(-20,ios:end);
If the seekg() function is used with
only one argument, the ios:beg
reference point is assumed. For
example in the statement:
iFi1.seekg(16);
ios:beg will be the reference point
and hence the get pointer will be
positioned 16 bytes from the beginning
of the file.
The tellg() member function does not
have any arguments. It returns the
current byte position of the get
pointer relative to the beginning of
the file. For example the statement:
intiPosition = iFi1.tellg();
will result in the variable iPosition
having the value of the current
position of the get pointer.
The seekp() and tellp() member
functions are identical to the above
two functions, but they are identified
with the put pointer. The seekg() and
tellg() member functions are defined
in the istream class. The seekp() and
tellp() member functions are defined
in the istream class. The seekp() and
tellp() member functions are defined
in the ostream class.
Program 20.1 finds out the number of
records in the file billfile.dat by
using the seekg() and tellg()
functions.
#include<fstream.h>
#include<iostream.h>
class bill
{
private:
intiBill_no;
floatfBill_amt;
public:
void getdata()
{
cout<<”Enter Bill number”;
cin>>iBill_no;
cout<<”Enter Bill amount”;
cin>>fBill_amt;
}
void showdata()
{
cout<<”Bill number ”<<iBill_no<<endl;
cout<<”Bill amount ”<<fBill_amt<<endl;
}
};
void main()
{
fstream Fi1(“billfile.dat”,ios::in);
Fi1.seekg(0,ios::end);
ini iEnd;
iEnd=Fi1.tellg();
cout<<”The size of the file is “<<iEnd<<endl;
cout<<”Size of one record is ”<<sizeof(bill)<<endl’
ini iNorec=iEnd/sizeof(bill);
cout<<”There are “<<iNored<<”records
in the file”<<endl;
}
In the given program, the statement:
Fi1.seekg(0,ios::end);
is used to reach the end-of-file. The
byte position is found by the
following statement:
iEnd=Fi1.tellg();
The value of iEnd is divided by the
size of one record to find out the
number of records in the file.
|