Learn about Input/Output & File Handling in C++



In the previous article, we have learned how to run operating system commands from C++ programs on UNIX/Linux machines. In the same context, it is also essential for C++ code to have the capability to deal with files for input and output. When you hear a saying like “Everything in UNIX is a file”, you will realize why file handling is of utmost importance.
That will be the subject of our today’s article. Wish you nice reading.

The term “Stream” refers to flow of data. So far, we have been using two objects: cin and cout for input and output. I told you earlier in this series that cin and cout represent the standard input stream and standard output stream, respectively.

Introducing the ios Class
The ios class is the base of all stream classes. One of the main capabilities offered by this class is formatting. Formatting is done using Manipulators and Switches (flags). In this section, we are going to see how to use both manipulators and flags to customize the input/output format.

As their name implies, manipulators are formatting operators that manipulates (controls) the output formatting. Although manipulators also work with input stream, most of the time they are used to customize the way output is displayed. Manipulators are used directly with the stream extraction >> and insertion << operators.

The following are some of the commonly-used formatting manipulators, with their descriptions and usage examples.
Inserts a newline character. This is equivalent to printing ‘\n’
Usage Example:

This prints the following output:


In output: the hex manipulator prints the hexadecimal equivalent of the number.
In Input: it treats the input as a hexadecimal number.

Usage Example
The following works like decimal to hexadecimal converter:

This program works as follows:


Notice that the variable num has been printed twice. In the former, it was printed normally as it is (in decimal format). In the latter, its hexadecimal equivalent is printed instead. The reason is that the second num was preceded by the hex manipulator. This draws a very important conclusion: a manipulator affects only data following it, not that coming before it.

Similar to hex, but accepts/prints the octal equivalent of the number.
Converts to the decimal equivalent.

Usage Example

This program works as follows:


The setw() manipulator function adjusts the field width in output.

Usage Example

This will print the following output:


the setfill() function specifies the padding (filling) character to use. This function is meaningful only with setw().

Usage Example

The number will be printed in 10-characters wide field, with blanks filled with #.

Format Flags are group of attributes defined in the ios class. These flags control the way input is interpreted, and output is printed.
Of the rich list of available format flags, we are going to discuss two commonly-used ones: left and right.
The left flag aligns output to left. Conversely, right aligns printed output to the right.

Consider the following code:

When executed, this code should produce the following formatted output:


The flags are set using the setf() function, and unset using the unsetf() function.

In this article, we had an introduction to I/O.

  • A Stream refers to flow of data.
  • Manipulators manipulate (control) the format of input and output.
  • Manipulators are inserted directly into streams.
  • Format Flags control the way input is interpreted, and output is printed. They are members of the ios

That was Part one in I/O and File Handling. See you in Part two.


Please enter your comment!
Please enter your name here