Fortunately, C++ makes it simple to transfer the skills you’ve acquired for keyboard input and display output (collectively termed
Text I/O and Text Files
Let’s re-examine the concept of text
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 digit 3, and the character code (in binary) for this digit is stored in ch. The input and the destination are both characters, so no translation is needed. (Note that it’s not the numeric value 3 that is stored; rather, it is the character code for the digit 3.) After the input statement, the digit character 8 is the next character in the input queue and will be the next character examined by the next input operation.
Next, let’s try the int type with the same input:
int n;
cin >> n;
In this case, cin reads up to the first non-digit character. That is, it reads the 3 digit and the 8 digit, leaving the period as the next character in the input queue. Then cin computes that these two characters correspond to the numeric value 38, and the binary code for 38 is copied to n.
Next, let’s try the double type:
double x;
cin >> x;
In this case, cin reads up to the first character that’s not part of a floating-point number. That is, it reads the 3 digit, the 8 digit, the period character, and the 5 digit, leaving the space as the next character in the input queue. Then cin computes that these four characters correspond to the numeric value 38.5, and the binary code (floating-point format) for 38.5 is copied to x.
Next, let’s try the char array type:
char word[50];
cin >> word;
In this case, cin reads up to the whitespace character. That is, it reads the 3 digit, the 8 digit, the period character, and the 5 digit, leaving the space as the next character in the input queue. Then cin stores the character code for these four characters in the array word and adds a terminating null character. No translation is needed.
Finally, let’s try an input variant for the char array type:
char word[50];
cin.geline(word,50);
In this case, cin reads up through the newline character (the sample input line had fewer than 50 characters). All the characters through the final 2 digit are stored in the array word, and a null character is added. The newline character is discarded, and the next character in the input queue will be the first character on the next line. No translation is needed.
On output, the opposite translations take place. That is, integers are converted to sequences of digit characters, and floating-point numbers are converted to sequences of digits and other characters (for example, 284.53 or -1.587E+06). Character data requires no translation.
The main point to this is that all the input starts out as text. Therefore, the file equivalent to console input is a text file—that is, a file in which each byte stores a character code. Not all files are text files. For example, databases and spreadsheets store numeric data in numeric forms—that is, in binary integer or binary floating-point form. Also, word processing files may contain text information, but they also contain non-text data to describe formatting, fonts, printers, and the like.
The file I/O discussed in this chapter parallels console I/O and thus should be used with text files. To create a text file for input, you use a text editor, such as Notepad for Windows, or vi or emacs for Unix/Linux. You can use a word processor, as long as you save the file in text format. The code editors that are part of IDEs also produce text files; indeed, the source code files are examples of text files. Similarly, you can use text editors to look at files created with text output.
Writing to a Text File
For file output, C++ uses analogs to cout. So to prepare for file output, let’s review some basic facts about using cout for console output:
• You must include the iostream header file.
• The iostream header file defines an ostream class for handling output.
• The iostream header file declares an ostream variable, or object, called cout.
• You must account for the std namespace; for example, you can use the using directive or the std:: prefix for elements such as cout and endl.