You are on page 1of 5

Q1 What are input and output stream?

ANS:- InputStream: InputStream is an abstract class of Byte Stream that


describe stream input and it is used for reading and it could be a file, image,
audio, video, webpage, etc. it doesn’t matter. Thus, InputStream read data
from source one item at a time.
OutputStream: OutputStream is an abstract class of Byte Stream that
describes stream output and it is used for writing data to a file, image, audio,
etc. Thus, OutputStream writes data to the destination one at a time.

InputStream OutputStream

1. It is an abstract class that 1. It is an abstract class that describes


describes Stream Input. Stream Output.

2. InputStream Read data from 2. OutputStream Write Data to the


the source once at a time. destination once at a time.

3. InputStream consist of
method which performs:
 Read next byte of data
from the input stream
and return -1 at the
3. Output Stream consists of methods
end of the file: public
which perform:
abstract int
read()throws  Write a byte to current
IOException Outputstream : public void
 Close current write(int)throws IOException
InputStream: public  Write array of byte to current
int available()throws output stream : public void
IOException write(byte[])throws
 Returns an estimate of IOException
the number of bytes  Flushes the current
that can be read from OutputStream: public void
the current input flush()throws IOException
stream: public void  Close current Output Stream.
close()throws : public void close()throws
IOException IOException

4. Types of InputStream are: 4. Types of OutputStream are:


 FileInputStream  FileOutputStream
InputStream OutputStream

 ByteArrayInputStream  ByteArrayOutputStream
 FilterInputStream  FilterOutputStream
 ObjectInputStream  ObjectOutputStream
In these types the most In these types the most important and
important and mostly used type mostly used type is FileOutput Stream.
is FileInputStream.

QUES:- What is the difference between opening a file with constructor function and opening a file
with open() function? When in one method preferred over the other?

ANS:- Opening a file with constructor function:

When an object is created then the constructor of a class initializes that object. In the same
way the file stream constructors initializes the file stream object once the fie stream objects
are created. Suppose if we want to open a file for reading the data then we make use of
Ifstream class and create its object and then we will use this input stream object for opening
the file and reading the data. For example;

Ifstream frdn (“mpfile”, ios::in);

The above statement creates an user defined object named frdn of input file stream class
Ifstream.This object opens the user defined filename impfile for reading the data from it.

Opening a file with open () function :

When we have multiple files to be opened one after the other in a sequence then we can make
use of open() function. We can create the object of file stream class once and use that object
to open multiple files. If we have to open multiple files for reading it then we can follow the
below example;

Ifstream frnd; //This Line creates an input stream frnd

frnd. open(“impfile”, ios::in); //This Line attaches frnd to impfile

//This Line processes the file

frnd. close(); //This is used for terminating the association of frnd with impfile

frnd. open(“imp1”,ios::in); //This Line attaches frnd to imp1

//This Line processes the file


Frnd. close(); //This is used for terminating the association of frnd with imp1

 When multiple files has to be opened using a single stream then open() function is more
preferred to be used. When single file has to be opened using single stream then opening
the file with constructor is more preferred.

QUES3:- Explain how while(fin) statement detects the end of a file that is connected to fin stream?

ANS:- To detect end of file, without using EOF(), we may check whether the stream object
has become NULL or not. For example, while(filin) { …… } The value in filin becomes zero
when the end of file is reached, and the loop ends. Since reading occurs inside the loop,
there will be no more attempts to read the file. Read more on Sarthaks.com -
https://www.sarthaks.com/436173/explain-how-while-filin-statement-detects-the-eof-for-a-
file-i-e-connected-to-filin-stream

Ques:- Write statements using seekg() to achieve the following:


(a) To move the pointer by 15 positions backward from current position.
(b) To go to the beginning after an operation is over.
(c) To go backward by 20 bytes from the end.

(d) To go to byte number 50 in the file


Ques

Qq

Ques

You might also like