Data File-4
Data File-4
SESSION - 4
FILE POINTERS AND THEIR MANIPULATION
In C++, the file I/O operations are associated with the two file pointers,
known as get pointer and the put pointer. They are useful in traversing the
opened file while reading or writing.
➢ ifstream, like istream, has a pointer known as the get pointer that points
to the element to be read in the next input operation.
➢ ofstream, like ostream, has a pointer known as the put pointer that points
to the location where the next element has to be written.
NOTE
➢ So the programmer need not bother about incrementing the file pointer
to the next location inside the file for the next action.
FILE POINTERS AND THEIR MANIPULATION ( CONTI…)
➢ Append mode
READ ONLY MODE
➢ This helps to read the file contents from the beginning (The bytes in a file
is numbered starting from zero).
WRITE ONLY MODE
➢ when a file is opened in a write only mode, the contents of file are
erased (if it exists) and the put pointer is set to the first byte of the file,
so that we can write data from the beginning.
APPEND MODE
➢ It would be necessary for us to add new data (or text) to the existing
file.
➢ In this case we have to open the file in append mode. When a file is
opened in a append mode, the put pointer moves to the end-of-file, so
that we write new data from that location.
FILE POINTERS AND THEIR MANIPULATION ( CONTI…)
Move the get pointer to a specified location from the beginning of a file.
There are two types:
➢seekg(long);
➢seekg(offset, seekdir);
The seekg(long) moves the get pointer to a specified location from the
beginning of a file.
SEEKG()
inf.seekg(20) ;
The above example tells that the get pointer points to 20th byte in a file
from 0th byte.
SEEKG(OFFSET, SEEKDIR)
➢ The offset indicates the number of bytes the get pointer is to be moved
from seekdir position.
➢ The offset takes long data type and seekdir (direction for seeking the
offset position inside a file) takes one of the following three seek
direction constants
SEEKG(OFFSET, SEEKDIR)
➢ Move the get pointer to the 0th byte (i.e., beginning of the file).
➢ Moves the get pointer to the 20th byte (i.e., from current
position of the file in forward direction).
SEEKG(OFFSET, SEEKDIR)
➢ The above example tells that the get pointer points to 20th byte in a
file from end of file in backward direction.
SEEKP()
Move the put pointer to a specified location from the beginning of a file.
There are two types:
➢ seekp(long);
➢ seekp(offset, seekdir);
➢ The seekp(long) moves the put pointer to a specified location from the
beginning of a file.
SEEKP()
inf.seekp(20) ;
➢The offset takes long data type and seekdir (direction for seeking the
offset position inside a file) takes one of the following three seek
direction constants.
SEEKP()
inf.seekp(20) ;
➢The above example tells that the put pointer points to 20th byte in a
file from 0th byte
➢ Move the put pointer to the 0th byte (i.e, beginning of the file) for
writing.
➢ Move the put pointer to the 20th byte (i.e, from current position of
the file in forward direction) for writing.
SEEKP()
➢ The above example tells that the put pointer points to 20th byte in
a file from end of file in backward direction.
TELLG( )
➢ The ifstream class provides the member function name tellg(). The
purpose of the function is to return current position of the get pointer.
int position;
Syntax:
position = fin.tellp();
BASIC OPERATION ON BINARY FILE IN C++
➢ Searching.
➢ Appending data.
➢ Inserting data in sorted files.
➢ Deleting a record
➢ Modifying data
THANK YOU