< previous page page_145 next page >

Page 145
Programs designed for noninteractive I/O do not print prompting messages for input. It is a good idea, however, to echo-print each data value that is read. Echo printing allows the person reading the output to verify that the input values were prepared correctly. Because noninteractive programs tend to print large amounts of data, their output often is in the form of a tablecolumns with descriptive headings.
Most C++ programs are written for interactive use. But the flexibility of the language allows you to write noninteractive programs as well. The biggest difference is in the input/output requirements. Noninteractive programs are generally more rigid about the organization and format of the input and output data.
File Input and Output
In everything we've done so far, we've assumed that the input to our programs comes from the keyboard and that the output from our programs goes to the screen. We look now at input/output to and from files.
Files
Earlier we defined a file as a named area in secondary memory that holds a collection of information (for example, the program code we have typed into the editor). The information in a file usually is stored on an auxiliary storage device, such as a disk (see Figure 4-2).
Our programs can read data from a file in the same way they read data from the keyboard. And they can write output to a disk file in the same way they write output to the screen.
Why would we want a program to read data from a file instead of the keyboard? If a program is going to read a large quantity of data, it is easier to enter the data into a file with an editor than to enter it while the program is running. With the editor, we can go back and correct mistakes. Also, we do not have to enter the data all at once; we can take a break and come back later. And if we want to rerun the program, having the data stored in a file allows us to do so without reentering the data.
Why would we want the output from a program to be written to a disk file? The contents of a file can be displayed on a screen or printed. This gives us the option of looking at the output over and over again without having to rerun the program. Also, the output stored in a file can be read into another program as input. For example, the Payroll program writes its output to a file named payFile. We can take payFile and read it into another program, perhaps one that prints out paychecks.

 
< previous page page_145 next page >