maandag 18 juni 2007

Simple File Input/Output part I

Sometimes keyboard input is not the best choice. For example, suppose you have written a program to analyze stocks, and you've downloaded a file of 1,000 stock prices. It would be far more convenient to have the program read the file directly than to hand enter all the values.
Similarly, it can be convenient to have a program write output to a file so that you have a permanent record of the results.

Fortunately, C++ makes it simple to transfer the skills you've acquired for keyboard input and output (collectively termed console I/O) to file input and output (file I/O). Later I will place more details about file I/O, but the book I read covers that at the end of the book.

But we'll look at simple text file I/O now.

Text I/O and Text Files
Let's reexamine 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 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 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 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:
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 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.getline(word, 50);

In this case, cin reads up to the newline character. 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 now 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 EDIT for DOS, Notepad for Windows, or vi or emacs for Unix/Linux.
The code editors that are part of IDE's 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.

File output is very much like console output:

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, 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;
- You can use cout with the << operator to write a variety of data types.

File output parallels this very closely:
- You must include the fstream header file
- The fstream header file defines an ofstream class for handling output
- You need to declare one or more ofstream variables, or objects, which you
can name as you please, as long as you respect the usual naming conventions.
- You must account for the std namespace; for example you can use the using directive or the std:: prefix for elements such as ofstream.
- You need to associate a specific ofstream object with a specific file; one way to do so id to use the open() method.
- When you're finished with a file, you should use the close() method to close the file.
- You can use an ofstream object with the << operator to output a variety of data types.

See the subject Simple File Input/Output part II

Geen opmerkingen: