unit 5
unit 5
Managing Console I/O Operations: Introduction - C++ Streams – C++ Stream Classes –
Unformatted I/O Operations – Formatted Console I/O Operation – Managing output with
Manipulators.
Working with Files: Introduction – Classes for File Stream Operators – Opening and closing a File –
Detecting end-of-file _ File Pointers and their Manipulators – Sequential Input and Output
Operations – Error Handling during File Operations – Command –Line Arguments.
Introduction
The process of giving data to the programs and getting result from the programs is called I/O
operation.
Q.No.1 C++ STREAMS
The flow of data between programs and the user through I/O devices is called streams.
There are two types of streams.
i) Input stream
ii) Output Stream
The stream that supplies data to the programs is called input stream.
The stream that receives the data from the program is called output stream.
In C++ there are number of stream classes for doing input-output operations. All these classes are
defined in the file iostream.h. The above figure shows the hierarchy of these classes.
ios class---- is topmost class in the stream classes hierarchy. It is the base class for istream,
ostream, and streambuf class.
iostream class-- is derived from both istream and ostream classes.. The class istream is used for
input and ostream for the output.
Class ios is indirectly inherited to iostream class using istream and ostream.
istream_withassign,ostream_withassign and iostream_withassign classes are inherited from
istream,ostream and iostream respectively.
iii) fill()
This function is used to fill the unused positions with the specified character. By default,
unused positions are filled with white spaces.
Syntax:
cout.fill(character);
Example:
int a=8;
cout.width(5);
cout.fill(‘@’)
cout<<a;
The output is,
@ @ @ @ 8
iv) setf()
It is used to set flags. Using this, we can display the numbers
* with various alignments
* in scientific and fixed form
* in decimal, octal and hexa decimal.
Syntax:
cout.setf(arg1,arg2);
arg1—flag to set
arg2---bit-field
Flag Bit-Field Used for
ios::left ios::adjustfield Left justified output
ios::right ios::adjustfield Right justified output
ios::internal ios::adjustfield Adding character like *,$ after sign
ios::scientific ios::floatfield Scientific notation
ios::fixed ios::floatfield Fixed point form
ios:: ios::basefield Decimal base
ios::oct ios::basefield Octal base
ios::hex ios::basefield Hexa decimal base
Example:
int a=-8;
cout.width(5);
cout.fill(‘@’)
cout.setf(ios::internal,ios::adjustfield);
cout<<a;
The output is,
- @ @ @ 8
Fig: Flags that do not have bit fields
float a =12.34;
cout.precision(3);
cout.width(5);
cout.setf(ios::showpos);
cout.setf(ios::showpoint);
cout<<b;
output:
+12.340
v)unsetf()
It is used to clear the flags set by the various formatting function.
Syntax:
cout.unsetf();
Q.No.4 MANAGING OUTPUT WITH MANIPULATORS
The header file iomanip provides a set of functions called manipulators which can be used to
format the output.
Manipulator meaning
setw(int w) Set the field width to ‘w’
setprecision(int d) Set the floating point precision to ‘d’
setfill(int c) Set the fill character to ‘c’
setiosflags(long l) Set the format flag f
sesetiosflags(long l) Clear the flag
endl Insert new line
Example:
int a=8;
cout<<setw(5);
cout<<setfill(‘@’)
cout<<a;
The output is,
@ @ @ @ 8
DESIGNING OUR OWN MANIPULATOR
We can design our own manipulator for certain special purposes.
Syntax:
ostream & manipulator(ostream & output)
{
Statements
return(output)
}
Example:
#include<iostream.h>
ostream &rupees(ostream &stream)
{
stream<<”Rs”;
stream.precision(2);
stream.setf(ios::showpoint);
return(stream)
}
void main()
{
cout<<rupees<<112.5;
getch();
}
Output:
Rs.112.50
WORKING WITH FILES:
Introduction
A file is a collection of related data stored in a disk. The flow of data between program and a
file is called stream. The stream that supplies data to the program is called input stream. The stream
that receives data from the program is called output stream.
Q.No.5 CLASSES FOR FILE STREAM OPERATIONS
C++ contains a set of classes that define the file handling methods.These include
ifstream,ofstream and fstream. These classes are derived from fstreambase ,istream,iostream and
ostream.
fstream----It supports both input and output operations. It inherits all the member functions from
istream, ostream, fstreambase and iostream.
i) Using constructor
i) Using constructor
Syntax:
Classname object(“filename”);
Where,
Classsname----ifstream or ofstream
Example:
ofstream f1(“stu.dat”);
It will open the “stu.dat” in output mode. In this mode, we can write data into that file.
Ifstream f2(“stu.dat”)
It will open the “stu.dat” in input mode. In this mode, we can read data from that file.
ofstream file1;
file1.open(“stu.dat”);
It will open the “stu.dat” in output mode. In this mode, we can write data into that file.
ifstream file2
file2.open(“stu.dat”)
closing a file:
Whenever the operations on a file are over it must be closed.The close() function is
used to close all the opened files.
Syntax:
Objectname.close();
Example:
file1.close();
file2.close();
Syntax:
Objectname.eof()
Example:
ifstream obj;
Obj.open(“stu.dat”);
while(obj!=0)
--------
}
Input pointer is used while reading data from the file and output pointer is used while writing data
into the file. Each time an input or output operation takes place, the appropriate pointer is
automatically updated.
i) seekg()
ii) seekp()
iii) tellg()
iv) tellp()
i) seekg()
This function is used to move the input pointer to a specified location.
Syntax:
Object.seekg(offset,refposition);
The refposition takes one of the following three constants.
Example:
fstream file;
file.open(“stu.dat”,ios::in);
file.seekg(10,ios::beg) // input file pointer is moved to the 10th byte from the beginning
ii) seekp()
This function is used to move the output pointer to a specified location.
Syntax:
Object.seekp(offset,refposition);
Offset--- number of bytes the file pointer to be moved.
Example:
fstream file;
file.open(“stu.dat”,ios::in);
file.seekp(10,ios::beg) // output file pointer is moved to the 10th byte from the beginning
iii) tellg()
It returns the current position of the input file pointer.
Syntax:
int p=object.tellg();
Example:
fstream file;
file.open(“stu.dat”,ios::in);
file.seekg(10,ios::beg)
iv) tellp()
It returns the current position of the output file pointer.
Syntax:
int p=object.tellp();
Example:
fstream file;
file.open(“stu.dat”,ios::in);
file.seekp(10,ios::beg)
Functions like put () and get() are designed for handling a single character at a time whereas write()
and read() are designed to write and read blocks of binary data.
i) get()
ii) read()
i) get()
Syntax:
Object.get(variablename);
Example:
fstream file;
file.open(“stu.dat”,ios::in);
while(file.eof()!=0)
file.get(ch);
cout<<ch;
ii) read()
This function is used to read a block of data from a file .It handles the data in binary form.
Syntax:
Object.read(char*)&var,sizeof(var));
Sequential output operations:
The sequential output operations can be performed by using the following functions
i) put()
ii) write()
i) get()
Syntax:
Object.put(variablename);
Example:
fstream file;
file.open(“stu.dat”,ios::in);
while(file.eof()!=0)
cin>>ch;
file.put(ch);
}
ii) write()
Syntax:
Object.write(char*)&var,sizeof(var));
Example program:
#include<iostream.h>
void main()
fstream file;
file.open(“stu.dat”,ios::in);
while(file.eof()!=0)
cin>>ch;
file.put(ch);
file.close();
fstream file;
file.open(“stu.dat”,ios::in);
while(file.eof()!=0)
file.get(ch);
cout<<ch;
file.close();
getch();
The above possible errors can be overcome by using the following member functions.
i) eof() -This function is used to check whether the end of file is reached or not. It returns true, if
end of file is reached; otherwise it returns false
ii) good() --This function is used to check whether there is any errors in a file. It returns a non-
zero (true) value when no error has occurred; otherwise returns zero (false).
iii) bad()---This function is used to check for any unrecoverable errors. If any unrecoverable errors
present, it returns true; otherwise it returns false.
iv) fail()---This function is used to check for any failures in input or output operation. It returns non-
zero (true) when an input or output operation has failed; otherwise it returns false
// statements
}
Example program:
#include<iostream.h>
void main(int argc, char *argv[])
{
printf("Number of arguments:\n", argc)
for(int i=0;i<=argc;i++)
{
cout<<argv[i]);
}
getch();
}
Terminal Input:
C:\> example.cpp object oriented programming
Arguments will be assigned as,
4rgc=4
argv[0]=example.cpp
argv[1]= object
argv[2]= oriented
argv[3]= oriented
Output:
Number of arguments:4
example.cpp
object
oriented
programming