0% found this document useful (0 votes)
11 views23 pages

Data File-4

Computer ppt

Uploaded by

ashwathparijatha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views23 pages

Data File-4

Computer ppt

Uploaded by

ashwathparijatha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

DATA FILE HANDLING

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

➢ When an input or output operation is performed, the appropriate


pointer is automatically advanced.

➢ 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…)

There are three modes under which we can open a file:

➢ Read only mode

➢ Write only mode

➢ Append mode
READ ONLY MODE

➢ When a file is opened in a read only mode, the get pointer is


automatically set to the very first byte (0th byte) of the file.

➢ 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…)

These internal stream pointers that point to the reading or writing


locations within a stream can be manipulated using the following member
functions:
➢ seekg()
➢ seekp()
➢ tellg()
➢ tellp()
SEEKG()

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 seekg(offset, seekdir) has two arguments: offset and 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)

 These constants are defined in ios class


SEEKG(OFFSET, SEEKDIR)

Example-1: inf.seekg(0, ios::beg);

➢ Move the get pointer to the 0th byte (i.e., beginning of the file).

Example-2: inf.seekg(20, ios:: cur);

➢ Moves the get pointer to the 20th byte (i.e., from current
position of the file in forward direction).
SEEKG(OFFSET, SEEKDIR)

Example-3: inf.seeg(-20, ios::end );

➢ 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 indicates the number of bytes the put 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.
SEEKP()

inf.seekp(20) ;

➢The above example tells that the put pointer points to 20th byte in a
file from 0th byte

➢The seekp(offset, seekdir) has two arguments offset and seekdir.


SEEKP()

Example-1: inf.seekp(0, ios::beg);

➢ Move the put pointer to the 0th byte (i.e, beginning of the file) for
writing.

Example-2: inf.seekp(20, ios:: cur) ;

➢ Move the put pointer to the 20th byte (i.e, from current position of
the file in forward direction) for writing.
SEEKP()

Example-3: inf.seekp(-20, ios:: end) ;

➢ 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.

Syntax: int position;


position = fin.tellg();
TELLP( )

➢ The ifstream class provides the member function name tellp( )


➢ The purpose of the function is to return current position of the put
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

You might also like