You have already used this
Up to this point, you have only captured data from the keyboard for your programs. However, programming can become quite a bit more interesting if you can acquire greater quantities of data with which to work, especially if you do not have to enter all the data each time you want to run the program. In this topic, you will learn how to access data from a text file, and how to place data in a text file. This will extend your ability to create larger and more interesting programs.
Data Streams
Up to this point, you have primarily been using utility functions for your console input and output (i.e., I/O). In the last group of topics related to iteration, you used the cin and cout functions as well as the extraction and insertion operators. Thanks to the C programming language developers, these are fairly straightforward keyboard input and monitor output operations.
Consider the following examples.
// prompt the user, get the first digit
// function: promptForInteger
firstDigit = promptForInteger( "Enter the first digit: " );
// prompt the user, get the second digit
// function: promptForInteger
secondDigit = promptForInteger( "Enter the second digit: " );
// add the digits
sum = firstDigit + secondDigit;
// output the result
// function: printString, printEndline
printString( "The sum of these two digits is: " + sum );
printEndline();
The operations in promptForInteger "connect" the keyboard to the program you are developing so when the person types the values into the keyboard, it follows the data stream through the operating system and into your specific program. Remember that the data stream is an abstraction representing all the hardware and software operations required to drive the input process. This is an excellent way to metaphorically understand the process without having to know everything about the "underground" stuff.
For the display part of the prompting action, and then for the display of the sum, you again have data in your program that must go out to the monitor. When you use the print operations in your specific program, it means that only your information will be displayed on only the monitor. Again, the data stream concept supports thinking about the "flow" of data without sweating the hardware and software details that are actually doing the business.
This stream abstraction, discussed in a previous chapter, is important for you to understand because it is also how data is acquired from other I/O devices, such as hard drives, removable drives (thumb drives, etc.), CD/DVD drives, and so on. And it is how data can be sent to output devices such as these drives, as well as others, such as printers, plotters, and other display devices.
The data stream concept for file I/O is identical to the one used for console input and output. The only difference is that for console I/O, you use the Console I/O utilities. That works because for any given program there will only be one keyboard and one monitor involved. As a side note, there could be other displays but we won't worry about them at this point.
Notice the word abstraction (again). You have already learned how to get data from a console stream (i.e., the keyboard), or to send data to a console stream (i.e., the monitor); and hard drives and removable disks are simply abstracted as, or acting like, other kinds of stream sources or destinations. As far as the computer is concerned, it is all the same.
As long as you understand that writing to a file or reading from a file is exactly the same process as sending data to a monitor or getting data from a keyboard, you will get along swimmingly. In the next topics, you will be looking at the syntax and operations for accessing the files but the actual act of outputting to a file or inputting from a file will look almost identical to the operations you have been using for a while.