System ProgrammingLearn about Input/Output & File Handling in C++ - Part-2

Learn about Input/Output & File Handling in C++ – Part-2


In Part one of File Handling in C++, we have introduced the ios class, the parent of all stream classes. We have learned some of its main features (manipulators, and format flags). In this article, we will revisit the ios class one more time, and then talk about the istream and ostream classes.

The ios Class Member Functions
Besides to the Manipulators and Format Flags, the ios class has a list of functions that control formatting. In this section, we are going to investigate some of the commonly-used functions, and illustrate them by example.

The setf() and unsetf() Functions
As I told you in the previous article, the setf() function is used to set (enable) format flags. Its opposite unsetf() disables the effect of the flag.

The fill() Function
This function works in two modes: it can get the fill character currently in use, or to set one.
Its use is equivalent to using the setfill() manipulator function.

The width() Function
Also having two modes: getting the current field width, and setting a new one. This is equivalent to using the setw() manipulator function.

Consider the following code that illustrates the use of fill() and width() functions:

#include <iostream>
using namespace std;
int main() 
	int num = 3490;
	char ch;
	cout << "Number: ";
	cout << num << endl;
	ch = cout.fill();
	cout << "Currently using " << ch << " as  filling character." << endl;
	return 0;

This program should produce the following output:

The istream and ostream Classes
Derived from the same parent class ios, both istream and ostream classes inherit the features of the ios class, and extend them with features of their own.

istream Class
As its name implies, this class is responsible for input operations. We have already used some of the functions defined in this class in many examples. For instance, we have been using the extraction operator >> since the first few articles. In this section, we are going to review on what we already know, and learn some new functions.

The Extraction Operator >>
Mostly used with the cin object (standard input stream), the extraction operator >> extracts input from the input stream on its left, and assigns it to the variable on its right.

The get() Function
We have learned about this function when talking about Strings. The get() function has many forms of operation depending on the list of provided arguments. For example, the function will wait for a single-character input, if provided a char variable as argument.

#include <iostream>
using namespace std;
int main() 
	char choice;
	cout   << "A) Print the hostname\n"
		 << "B) Print the IP Address info\n"
		 << "C) Print the system data and time\n\n"
		 << "Enter your choice: ";
	cout << "\nYou entered " << choice << endl;
	return 0;	

This will behave as follows:
The get() function could be also used to read a single-line input, or multi-line input if a delimiter character is specified.

The getline() Function
Another way to extract input text. This function is similar to the get() function, with slightly different syntax.

Consider the following program:

#include <iostream>
using namespace std;
int main()
	string str;
	char delimit = '#';
	cout << "\n Enter text: \n";
	getline(cin, str, delimit);
	cout << "\nYou entered: \n" << str << endl;
	return 0;

This will read user input until the user enters #.

The gcount() Function
This function returns the number of characters read in the last input operation.

ostream Class
In contrary with istream class, the ostream class is responsible for output operations. We have already used one of the utilities defined in this class: the insertion operator << that we used so far in this series to write to the standard output.

Besides to the extraction operator and the other functions inherited from its parent class ios, like fill(), clear(), and good(), the ostream class has methods of its own like:
put() and write() for unformatted output.
tellp() and seekp() for getting and setting position inside a file.

In this article, we have continued what we started in Part one in the context of I/O.

  • The fill() function gets/sets the padding character in output.
  • The width() functions gets/sets field width.
  • The istream and ostream classes are derived from the ios class.

That was Part two. See you in Part three, and File I/O.


Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -Newspaper WordPress Theme

Latest article


More article

- Advertisement -Newspaper WordPress Theme