Page 270 - DCAP404 _Object Oriented Programming
P. 270
Unit 12: Console I/O
converts them to internal form. Formatted I/O can be either “Free” format or “Explicit” format, Notes
as described below.
Advantages and Disadvantages of Unformatted I/O
Unformatted input/output is the simplest and most efficient form of input/output. It is usually
the most compact way to store data. Unformatted input/output is the least portable form of
input/output. Unformatted data files can only be moved easily to and from computers that
share the same internal data representation. It should be noted that XDR (eXternal Data
Representation) files, described in Portable Unformatted Input/Output, can be used to produce
portable binary data.
Did u know? Is unformatted input/output is not directly human readable?
Unformatted input/output is not directly human readable, so you cannot type it out on a
terminal screen or edit it with a text editor.
Advantages and Disadvantages of Formatted I/O
Formatted input/output is very portable. It is a simple process to move formatted data files to
various computers, even computers running different operating systems, as long as they all use
the ASCII character set. (ASCII is the American Standard Code for Information Interchange. It is
the character set used by almost all current computers, with the notable exception of large IBM
mainframes.) Formatted files are human readable and can be typed to the terminal screen or
edited with a text editor.
However, formatted input/output is more computationally expensive than unformatted input/
output because of the need to convert between internal binary data and ASCII text. Formatted
data requires more space than unformatted to represent the same information. Inaccuracies can
result when converting data between text and the internal representation.
Formatted Console I/O Operations
C++ supports a number of features that could be used for formatting the output. These features
include:
1. ios class functions and flags.
2. Manipulators.
3. User-defined output functions.
The ios class contains a large number of member functions that could be used to format the
output in a number of ways. The most important ones among them are listed below.
Function Task
width() To specify the required field size for displaying an output value
precision() To specify the number of digits to be displayed after the decimal point of a float value
fill() To specify a character that is used to fill the unused portion of a field.
setf() To specify format flags that can control the form of output display (such as Left-
justification and right-justification).
unsetf() To clear the flags specified.
LOVELY PROFESSIONAL UNIVERSITY 263