0% found this document useful (0 votes)
34 views

File Pointers2938

File handling in C++ uses streams to represent the flow of data to and from files. There is a stream class hierarchy with base classes like iostream for input/output streams. Different file operations like opening, reading, writing and checking the end of a file can be performed. File pointers track the read/write position in a file and functions like seekg() and tellg() can set and get the read pointer position, while seekp() and tellp() work with the write pointer.

Uploaded by

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

File Pointers2938

File handling in C++ uses streams to represent the flow of data to and from files. There is a stream class hierarchy with base classes like iostream for input/output streams. Different file operations like opening, reading, writing and checking the end of a file can be performed. File pointers track the read/write position in a file and functions like seekg() and tellg() can set and get the read pointer position, while seekp() and tellp() work with the write pointer.

Uploaded by

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

FILE HANDLING IN

C++

STREAMS
A Stream is a general name given to flow of
data.
Different streams are used to represent
different kinds of data flow.
Each stream is associated with a particular
class, which contains member functions and
definitions for dealing with that particular
kind of data flow.

The Stream Class Hierarchy
ios
istream
get()
getline()
read()
>>
ostream
put()
write()
<<

fstreambase
iostream
Ifstream
Open()
Tellg()
Seekg()
Ofstream
Open()
Tellp()
Seekp()
fstream
NOTE : UPWARD ARROWS INDICATE
THE BASE CLASS
DIFFERENT FILE OPERATIONS
OPENING A FILE
CLOSING A FILE
READING FROM A FILE
WRITING ON A FILE
CHECKING FOR END OF FILE

File Mode Parameters
PARAMETER MEANING
Ios::app Append to end-of file
Ios::ate goto end of file on opening
Ios::binary binary file
Ios::in
Ios::nocreate open fails if file doesnt exist
Ios::noreplace open fails if file already exists
Ios::out
Ios::trunc Deletes contents if it exists
FILE POINTERS
FILE POINTERS
Each file object has two integer values
associated with it :
get pointer
put pointer
These values specify the byte number in the
file where reading or writing will take
place.

File pointers..
By default reading pointer is set at the
beginning and writing pointer is set at the
end (when you open file in ios::app mode)

There are times when you must take control
of the file pointers yourself so that you can
read from and write to an arbitrary location
in the file.
Functions associated with file
pointers :

The seekg() and tellg() functions allow you
to set and examine the get pointer.

The seekp() and tellp() functions allow you
to set and examine the put pointer.
seekg() function :
With one argument :
seekg(k) where k is absolute position from
the beginning. The start of the file is byte 0

Begin
File
End
k bytes
^
File pointer
The seekg() function with one argument
seekg() function :
With two arguments :
the first argument represents an offset from a
particular location in the file.
the second specifies the location from which
the offset is measured.
Begin End
^
Offset from Begin
The seekg() function with two argument
seekg() function :
With two arguments :
Begin
End
^
Offset from Begin
The seekg() function with two argument
^
^
Offset from end
Offset from current
position

You might also like