Chapter Four-File Processing v1.0
Chapter Four-File Processing v1.0
processing
Files and Streams
Is_open():
returns true if
the file has been
opened
successfully and
false if it has
failed.
Example : file handling using
ifstream and ofstream
Activties
▪ A Sequential file has to be Accessed in the same order the file was
written.
▪ Let's take an example of Cassette Tapes: We play music in the same
order it was recorded. we can Quickly fast-forward or rewind over
songs we don't want to listen to, but the order of the songs dictates
what we do to play the song we want. But it is difficult, and
sometimes impossible, to insert data in the middle of two other
songs on a tape. The Only way to truly add or delete records from
the middle of a Sequential file is to create a completely New file
that combines both old and new records.
▪ A file created with the help of C++ standard library functions does
not impose any structure on how the data is to be persisted.
However, we are able to impose structure programmatically
according to the application requirement.
Opening Sequential files
▪ So far, all of the file access we’ve done has been sequential --
that is, we’ve read or written the file contents in order. However,
it is also possible to do random file access -- that is, skip around
to various points in the file to read its contents. This can be
useful when your file is full of records, and you wish to retrieve a
specific record. Rather than reading all of the records until you
get to the one you want, you can skip directly to the record you
wish to retrieve.
▪ Once you have a file open for processing, you can navigate to
different parts of the file. This is often referred to as random
access of files. It really means that you aren't at the beginning or
the end. Two functions are available:
– istream: seekg() (or seek and get)
– ostream: seekp() (seek and put)
Random file access