Define input stream and output stream

Estudies4you
Q6. Define input stream and output stream.
Answer:
Input Stream
The input stream is used to read input through standard input device, keyboard. It uses a predefined stream object cin to perform the console read operation.
The cin object uses extraction operator (>>) to perform the input operation and this operator is stored in istream class.
Syntax
cin>> variable_name;
Output Streams
The output stream is vised: to control the output through standard cute devices, monitor. It uses a predefined stream object cout to display the output. The cout object use insertion operator << to perform the console write operation and this operator is stored in ostream class.
Syntax
cout <<“output statement”;
cout << variable_name;

Q7. Write about overloading insertion and extraction operators.
Answer:
In C++, the operator << is called an insertion operator because it inserts the characters into a stream and the operator >> is called an extraction operator because it extracts characters from a stream. Like other operators such as +, ⧿, *, etc. the operators >> and << can also be overloaded. The functions that overload the insertion and extraction operators are called inserter and extractor respectively.
The prototypes definition for insertion (<<) and extraction (>>) operators are shown below,
Prototypes
friend istream& operator>>(istream& streamobj, ClassName& classobj);
friend ostream& operator<<(ostream& streamobj, const ClassName& classobj);

Q8. What are the predefined streams in C++?
Answer:
Predefined streams are those streams that are embedded in the system by default. These streams get activated, when the program execution starts.
The multiple predefined streams that are available in C++ are,
(a) cin
cin is an input object, which takes input through standard input device, keyboard. It is as similar to stdin in C. It uses extraction operator >> to read data.
(b) cout
cout is an output object, which gives output to standard output device i.e., monitor. It is as similar to stdout in C. It uses insertion operator << to display data.
(c) cerr
cerr is a standard error object, which is used to manage the errors in unbuffered data. This object passes the errors that are occurred in unbuffered data to standard output device i.e., monitor. Its functionality is similar to stderr in C.
(d) clog
clog is often a standard error object, but it manages the errors that takes place in buffered data. This type of pre defined stream is not supported by C.

Q9. Discuss in brief about ifstream class.
Answer:
ifstream Class
This class supports input operations on the files. It inherits the functions get(), getline(), read(), seekg() and tellg() from istream class. When the file is opened in default mode, it contains open() function.
The functions of ifstream class are as follows,
(i) get()
This function is used to read a single character from the file. The get pointer specifies the character which has to be read from the file. This function belongs to fstream class.
(ii) getline()
This function reads a line of text which ends with a newline charater. It can be called using cin object as follows,
cin.getline( line, size)
Where line is a variable that reads a line of text.
Size is number of characters to be read getline() function reads the input until it encounters ‘\n’ or size -1 characters are read. When ‘\n’ is read it is not saved instead it is replaced by the null character.
(iii) read()
This function reads a block-of data of length specified by an argument ‘n’. This function reads data sequentially. So when the EOF is reached before the whole block is read then the buffer will contain the elements read until EOF.
General syntax,
istream & read(char*str, size n);
(iv) seekg()
This function is used to shift the input pointer (i.e., get) to the given location. It belongs to ifstream. It is a file manipulator.
(v) tellg()
This function is used to determine the current position of the input pointer. It belongs to ifstream class.

Q10. Write in short about open() and close() function class.
Answer:
open()
The open() function is used to create new files as well as open existing files.
General Form
Stream object open (“file name”, Mode); where ‘mode’ specifies the purpose of opening a file. This ‘mode’ argument is optional, if it is not given, then the prototype of member functions of the classes ifstream and ostream uses the default values for this argument. The default value are,
ios:: in for ifstream functions i.e., open in read only mode
and. ios:: out for ofstream functions i.e., open in write only mode.
close()
A file can be closed using member function close(). This function neither takes any parameter nor returns any value. .
Syntax
stream_name.close();
Here,

stream_name is the name of the stream to which file is linked.

To Top