Text I/O and Text Files

Let’s re-examine the concept of text I/O. When you use cin for input, the program views input as a series of bytes, with each byte being interpreted as a character code. No matter what the destination data type, the input begins as character data—that is, text data. The cin object then has the responsibility of translating text to other types. To see how this works, let’s examine how different code handles the same line of input.

Suppose you have the following sample line of input:

38.5 19.2

Let’s see how this line of input is handled by cin when used with different data types. First, let’s try type char:

char ch;cin >> ch;

The first character in the input line is assigned to ch. In this case, the first character is the ...

Get C++ Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.