Unit-IV Oop Part-I RBK
Unit-IV Oop Part-I RBK
istream ostrea
fstreambase
get() m
getline( put()
) read() iostrea write()
>> m <<
Ifstrea Ofstrea
fstream
m m
Open() Open()
Tellg() Tellp()
Seekg() Seekp()
We turn our computers on and off
◦ The contents of our main memory is transient
We like to keep our data
◦ So we keep what we want to preserve on disks and similar
permanent storage
A file is a sequence of bytes stored in permanent
storage
◦ A file has a name
◦ The data on a file has a format
We can read/write a file if we know its name and
format
0: 1: 2:
iostreams
Objects
Files
(of various types)
(sequences of bytes)
Program
Input Outp ut
Stream
Stream
Files
To perform input and output, a C + + program:
1. Construct a stream object.
2. Connect (Associate) the stream object to an
actual IO device (e.g., keyboard, console, file,
network, another program).
3. Perform input/output operations on the stream,
via the functions defined in the stream's pubic
interface in a device independent manner.
4. Disconnect (Dissociate) the stream to the actual
IO device (e.g., close the file).
5. Free the stream object.
Streams act as an interface between files and
programs.
They represent as a sequence of bytes and
i/p operation on
array bloked
stream
Holds one non
derived data
member
Convenient way to deal large quantities of data.
Store data permanently (until file is deleted).
Avoid typing data into program multiple times.
Share data between programs.
We need to know:
k ^
bytes File pointer
^
Offset from
Begin
^
Offset from Begin
^
Offset from end
^
Offset from
current
position
infile.seekg(10); =>
Moves the file pointer to the byte number 10.
The bytes in a file are numbered beginning from zero.
Thus, the pointer will be pointing to the 11th byte in the file.
The refposition takes one of the following these constant defined in the
ios class.
ios::beg start of the file
ios::cur current position of the pointer
ios::end end of the file.
Sometimes during file operations, errors may also
occur
For example, a file being opened for reading might
not exist.
Or a file name used for a new file may already exist.
Or an attempt could be made to read past the end-of-
file. Or
such as invalid operation may be performed.
There might not be enough space in the disk for
storing data.
To check for such errors and to ensure smooth
processing, C + + file streams inherit 'stream-state'
members from the ios class that store the information
on the status of a file that is being currently used.
The current state of the I/O system is
held in an integer, in which the following
flags are encoded :
There are several error handling functions supported by class
ios that help you read and process the status recorded in a
file stream .
open()
An open file is represented within a program by
a stream and any input or output operation
performed on this stream object will be applied to
the physical file associated to it .
In order to open a file with a stream object we use its
member function open:
Syntax:-
fstream fin;
fin.read( (char *) &obj,
sizeof(obj) );
&obj : Initial byte of an object stored in file.
sizeof(obj) : size of object represents the
total number of bytes to be read from
initial byte.
The read() function returns NULL if no data
read.
Command-line arguments are given after
the name of the program in command-line
shell of Operating Systems.
To pass command line arguments, we
Output:-
You have entered 4 arguments:
./main
SE
Div3
Class
They are passed to main() function.
They are parameters/arguments supplied to
the program when it is invoked.
They are used to control program from
outside instead of hard coding those
values inside the code.
argv[argc] is a NULL pointer.
argv[0] holds the name of the program.
argv[1] points to the first command line
argument and argv[n] points last argument.
You pass all the command line arguments
separated by a space, but if argument
itself has a space then you can pass such
arguments by putting them inside double
quotes “” or single quotes ”.
Example:-
#include<stdio.h>
// Driver code
int main()
{
cerr << "the message displayed is
unbuffered";
return 0;
}
Output:
This is also an instance of iostream class
and used to display errors .
but unlike cerr the error is first inserted into a
buffer and is stored in the buffer until it is
not fully filled.
The error message will be displayed on
the screen too.
// c++ program to implement the cerr
#include <iostream>
using namespace std;
// Driver code
int main()
{
clog << "the message displayed is buffered";
return 0;
}
Output:
cerr is the standard error stream which is used to
output the errors.
It is an instance of the ostream class.
As cerr stream is un-buffered so it is used when
we need to display the error message
immediately and does not store the error
message to display later.
The object of class ostream that represents the
standard error stream oriented to narrow
characters(of type char). It corresponds to the C
stream stderr.
The “c” in cerr refers to “character” and ‘err ’
means “error”, Hence cerr means “character
error”. It is always a good practice to use cerr to
display errors
/ / C + + program to illustrate std::cerr
#include <iostream>
using namespace std;
/ / Driver Code
int main()
{
oj=77
od=890.12
ostr1=Ram
ostr2=Sita
istr1=
istr2=Q8V
Overloading the Extraction and
Insertion Operators
The operator >> is a Bitwise Right
operator. The operator is overloaded in C+
+ for ‘cin’ to input data to standard input
device.
The operator is then called as Extraction
Operator because it extract data from input
device (or standard input stream).
The operator << is a Bitwise Left operator.
The operator is overloaded in C++ for ‘cout’
to output data to standard output device.
The operator is then called as Insertion
Operator because it Insert data into output
Overloading the Extraction and
Insertion Operators
These operators are overloaded by most of
the file/stream classes to read data from
streams or write data into streams.
Insertion Operator << is used to Write
(send) data in program to stream.
Extraction Operator >> is used to Read
(receive) data from stream to program.
Syntax:
ostream&operator<<(ostream&stream,
Typeobj)
{//body of inserter
returnstream;
}
Overloading the Extraction and
Insertion Operators
We can overload the operator for our
class object to output data from object.