Unit_6_ConsoleI_Operation&Working_with_Files
Unit_6_ConsoleI_Operation&Working_with_Files
C++ streams
In C++, a stream refers to a sequence of characters that are transferred between the program
and input/output (I/O) devices. Stream classes in C++ facilitate input and output operations
on files and other I/O devices. These classes have specific features to handle program input
and output, making it easier to write portable code that can be used across multiple
platforms.
To use streams in C++, you need to include the appropriate header file. For instance, to use
input/output streams, you would include the iostream header file. This library provides the
necessary functions and classes to work with streams, enabling you to read and write data to
and from files and other I/O devices.
Stream Classes
ios Class
ios class is the highest class in the entire hierarchical structure of the C++ stream. It is also
considered as a base class for istream, stream, and streambuf class. It can be said that the ios
class is basically responsible for providing all the input and output facilities to all the other
classes in the stream class of C++.
istream Class
istream being a part of the ios class which is responsible for tackling all the input stream
present within the stream. It provides all the necessary and important functions with the
number of functions for handling all the strings, chars, and objects within the istream class
which comprises all these functions such as get, read, put, etc.
ostream Class
This class as part of the ios class is also considered as a base class that is responsible for
handling output stream and provides all the necessary functions for handling chars, strings, and
objects such as put, write, etc.
iostream Class
iostream class is the next hierarchy for the ios class which is essential for input stream as well as
output stream because istream class and ostream class gets inherited into the main base class.
As the name suggests it provides functionality to tackle the objects, strings, and chars which
includes inbuild functions of put, puts, get, etc.
The ios class contains a large number of member functions that would help us to format the
output in some ways.
Function Task
width() It specifies the required field size for displaying an output value.
precision() It specifies the number of digits to be displayed after the decimal point of a float value.
fill() It specifies a character that is used to fill the unused portion of a field.
setf() It specifies format flags that can control the form of the output display.
unsetf() It is used to clear the flags specified.
Unformatted input/output functions
Unformatted console input/output functions are used for performing input/output operations
at console and the resulting data is left unformatted and untransformed i.e. it is left in its raw
and original form.
In C++, we can read the input entered by a user at console using an object cin of istream class
and through this object we can access the functions of istream class, such as - get(char *),
get(void) and getline().
In C++, we can write the output at console using an object cout of ostream class and through
this object we can access the functions of ostream class, such as - put(), write().
Functions Description
Reads a single character from the user at the console and assigns it
get(char *)
to the char array in its argument, but needs an Enter key to be
pressed at the end..
Reads a single character from the user at the console, and returns
get()
it.
write(char *arr, int num) Writes a number of characters in a char array to the console.
Manipulators
It is a special function that can be included in IO statements to alter the format parameters of
stream. To access manipulators the file <iomanip> used
The iomanip header file contains several special functions that are used to perform formatted
IO operations.
The following table provides the details of the special parameterized manipulator functions
used to perform formatted IO in C++.
Function Description
setw(int) Used to set the width in number of characters for the immediate output data.
setfill(char) Used to fill the blank spaces in output with given character.
setprecision(int) Used to set the number of digits of precision.
setbase(int) Used to set the number base.
setiosflags(format flags) Used to set the format flag.
resetiosflags(format flags) Used to clear the format flag.
The iomanip also contains the following nom parameterized manipulators format flags using in
formatted IO in C++.
Flag Description
endl Used to move the cursor position to a newline.
ends Used to print a blank space (null character).
dec Used to set the decimal flag.
oct Used to set the octal flag.
hex Used to set the hexadecimal flag.
left Used to set the left alignment flag.
right Used to set the right alignment flag.
showbase Used to set the showbase flag.
noshowbase Used to set the noshowbase flag.
showpos Used to set the showpos flag.
noshowpos Used to set the noshowpos flag.
showpoit Used to set the showpoit flag.
noshowpoint Used to set the noshowpoint flag.
Command-line arguments are the values given after the name of the program in the
command-line shell of Operating Systems. Command-line arguments are handled by the
main() function of a C/C++ program.
To pass command-line arguments, we typically define main() with two arguments: the first
argument is the number of command-line arguments and the second is a list of command-
line arguments.
argc (ARGument Count) is an integer variable that stores the number of command-line
arguments passed by the user including the name of the program. So if we pass a value to
a program, the value of argc would be 2 (one for argument and one for program name)
The value of argc should be non-negative.
argv (ARGument Vector) is an array of character pointers listing all the arguments.
If argc is greater than zero, the array elements from argv[0] to argv[argc-1] will contain
pointers to strings.
argv[0] is the name of the program , After that till argv[argc-1] every element is command
-line arguments.