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

Module 4

C++
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Module 4

C++
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Module-4

In C++ there are number of stream classes for defining various streams
related with files and for doing input-output operations. All these classes are
defined in the file iostream.h. Figure given below shows the hierarchy of
these classes
1.

ios class is topmost class in the stream classes hierarchy. It is the base class
for istream, ostream, and streambuf class.

2. istream and ostream serves the base classes for iostream class. The
class istream is used for input and ostream for the output.

3. Class ios is indirectly inherited to iostream class using istream and ostream. To
avoid the duplicity of data and member functions of ios class, it is declared as virtual
base class when inheriting in istream and ostream as

4. class istream: virtual public ios


5. {
6. };
7. class ostream: virtual public ios
8. {
9. };
acilities provided by these stream classes.
1. The ios class: The ios class is responsible for providing all input and output facilities
to all other stream classes.

2. The istream class: This class is responsible for handling input stream. It provides
number of function for handling chars, strings and objects such as get, getline, read,
ignore, putback etc..
The ostream class: This class is responsible for handling output stream. It provides
number of function for handling chars, strings and objects such as write, put etc..
3. The iostream: This class is responsible for handling both input and output
stream as both istream class and ostream class is inherited into it. It
provides function of both istream class and ostream class for handling
chars, strings and objects such as get, getline, read, ignore, putback,
put, write etc..
File stream
File handling is used to store data permanently in a computer. Using file
handling we can store our data in secondary memory (Hard disk).
How to achieve the File Handling
For achieving file handling we need to follow the following steps:-
STEP 1-Naming a file
STEP 2-Opening a file
STEP 3-Writing data into the file
STEP 4-Reading data from the file
STEP 5-Closing a file.

Streams in C++ :-

We give input to the executing program and the execution program gives back the output.
The sequence of bytes given as input to the executing program and the sequence of bytes
that comes as output from the executing program are called stream. In other words,
streams are nothing but the flow of data in a sequence.
The input and output operation between the executing program and the devices like
keyboard and monitor are known as “console I/O operation”. The input and output
operation between the executing program and files are known as “disk I/O operation”.

C++ Files
The fstream library allows us to work with files.

To use the fstream library, include both the


standard <iostream> AND the <fstream> header file:

#include <iostream>
#include <fstream>
There are three classes included in the fstream library, which are used to
create, write or read files:

Class Description

ofstream Creates and writes to files

ifstream Reads from files

fstream A combination of ofstream and ifstream: creates, reads, and writes to files

Create and Write To a File


To create a file, use either the ofstream or fstream class, and specify the name of
the file.

To write to the file, use the insertion operator (<<).

Read a File
To read from a file, use either the ifstream or fstream class, and the name of the file.

Binary Files with C++


The insertion and extraction operators (i.e. << and >> are meant to be used by
programs for writing to and reading from text files; it is assumed that the
programmer is familiar with the differences between these two file formats.
Basics of File I/O

Opening a Stream
The ifstream and ofstream each have member functions named open which are used to
attaching the stream to a physical filename and opening the file for either reading or
writing. The open member function also provides for a couple of optional arguments that are not
often described. The most general prototype of this function is

void open(const char *filename[, int mode][, int prot]);

Writing to a Binary File


I mentioned once that << is used to write data to a text file. If you had a
variable x that contained the value 354 and you used the statment outfile << x; this
would cause the character 3, the character 5, and the character 4 to be written (in
ASCII form) to the file. This is not binary form which would only require 16-bits.
The ofstream class provides a member function named write that allows for
information to be written in binary form to the stream. The prototype of
the write function is
ostream& write(void *buffer, streamsize n);

Reading from a Binary File


Reading data from a binary file is just like writing it except that the function is now
called read instead of write When reading data from a file there are a couple of new
things to watch out for:

 It is the responsibility of the programmer to make sure that the buffer is large
enough to hold all the data that is being read

File Pointer

The tellp() member function has a prototype of the form


streampos tellp();

This function accepts no parameters, but returns the location given in bytes from the
beginning of the file where the file pointer is currently sitting. The next read or write
will take place from this location.

The seekp() member function has a prototype of the form


void seekp(streampos location, int relative);

1. ios::beg This indicates that the location is the number of bytes from the
beginning of the file.
2. ios::cur This indicates that the location is the number of bytes from the current
file pointer location. This allows for a relative positioning of the file pointer.
3. ios::end This indicates that the location is the number of bytes from the end of
the file.

Writing Classes to Binary Files


The easiest way to store records in files is to use astruct If you are keeping track of
records in memory structures using classes, then saving these classes to disk takes
a little extra work.

You might also like