What is an unformatted IO operation in C plus plus?
In simple terms, unformatted input and output is the most
primitive form of input and output. It typically offers the most
compact storage but is generally less portable than formatted input
and output.
If we consider the decimal value 1.23, there are various ways we
can output this value. We might choose to use unformatted output
using IEEE 754 encoding, creating the 4-byte binary value
0x3f9d70a4. However, in order to input this value upon another
system, we must first tell that other system to expect an IEEE 754
value otherwise the binary value could be interpreted as being the
integer value 1,067,282,596 or the character sequence "? p ¤ " or
something else entirely.
With formatted input and output, instead of the value 1.23 we
output the null-terminated string "1.23", thus creating the 5-byte
binary value 0x312e323300. When we come to input that value, the
system does not need to know the specifics of the encoding, it
simply needs to know that the input is formatted. Thus we read back
the string "1.23", which can then be converted to the floating
point value 1.23 using whatever encoding the system actually
supports.
Humans typically input data in formatted form using character
sequences. So when entering the value 1.23 from the keyboard, we
generate the string "1.23". If the system knows that it is
expecting a floating point value, then it will attempt to convert
the string accordingly, creating unformatted data from the
formatted input. Similarly, when the computer presents the
unformatted value 1.23 as output to the user, it is converted back
to a string creating formatted output from the unformatted
data.
When we create formatted output from unformatted output, we can
also choose to decorate the output, such that the unformatted value
1.23 might be represented as the formatted value "£1.23" if the
value happens to represent a UK currency value. Similarly, we can
do a reverse conversion when the user inputs the formatted value
"£1.23".