CSCI 1300 - Exercise 5
Input and Output from Files

What You'll Get from This Exercise

Until now, all of the input and output of your programs has been with the keyboard and monitor. You have used cin for the input stream and cout for the output stream. In this exercise, you’ll learn how to use a disk file for input or output.

PC Labs in the
Engineering Center
CR 235    CR 239
CR 244    CR 252 (24 hours)
CH 107    ME 107
Other campus sites are
listed at www.colorado.edu/its/labs.

Installing the
CS1300 Software
Open a DOS window. If the CS1300 software is not permanently
installed on your machine, then use one of these methods:
If you get the message "Out of environment space", then:
  • Click on the MS-DOS icon in the top left corner of the window.
  • Select Properties from the pop-up menu.
  • Select the Memory tab from the command box.
  • Click the arrow on the Initial Environment box. Move down in the box as far as possible (by clicking the downward arrow). Click on the biggest number that you see in this box.
  • Click OK in the command box.
  • Click OK in the MS-DOS Prompt information box.
  • Stop the DOS Session by clicking the X in the top-right or by typing the command "exit".
  • Restart a new DOS session and try running your commands again.

A Program to Illustrate File Input/Output

You'll start this exercise by getting and running a file demonstration program. You can copy copyints.cxx from your installed cs1300\lab\ directory to your working directory. Also copy two files named numbers.dat and message.dat, from the same directory. One of these files contains a secret message!

The Purpose of copyints.cxx

Using the copyints Program without Changes

Improving the copyints Program

Summary: How to Open a File for Input

  1. Make sure that you have the include directive to include <fstream.h>.
  2. Declare two variables: a sequence of characters to hold the name of the input file, and a variable (often called ins) to act as the input file stream. For example:
    char in_file_name[40];
    ifstream ins;
    
  3. Prompt the program’s user for the name of the input file, and read this name. For example:
    cout << "Please type the name of the input file: ";
    cin >> in_file_name;
    
  4. Open the input file, attaching it to the input file stream. Make sure to check that there was no problem opening the file, like this:
    ins.open(in_file_name);
    if (ins.fail( ))
    {
        cerr << "Could not open " << in_file_name << "!" << endl;
        return EXIT_FAILURE;
    }
    
  5. After the input file has been opened, the input file stream ins can be used in exactly the same way that you use the standard input cin. Characters, integers, and other data types can all be read from ins. When the program ends, you should close the input file stream with the statement: ins.close();

Summary: How to Open a File for Output:

  1. Make sure that you have the include directive to include <fstream.h>.
  2. Declare two variables: a sequence of characters to hold the name of the output file, and a variable (often called outs) to act as the output file stream. For example:
    char out_file_name[40];
    ofstream outs;
    
  3. Prompt the program’s user for the name of the output file, and read this name. For example:
    cout << "Please type the name of the output file: ";
    cin >> out_file_name;
    
  4. Open the output file, attaching it to the output file stream. Make sure to check that there was no problem opening the file, like this:
    outs.open(out_file_name);
    if (outs.fail( ))
    {
        cerr << "Could not open " <<< out_file_name << "!" << endl;
        return EXIT_FAILURE;
    }
    
  5. After the output file has been opened, the output file stream outs can be used in exactly the same way that you use the standard output cout. Characters, integers, and other data types can all be written to outs. When the program ends, you should close the output file stream with the statement: outs.close();

Decoding a Secret Message

Modify the program so that instead of writing the numbers to the output file, it writes the characters that each number stands for from the ASCII character code. This code assigns one letter to each number from 0 to 127. For example, the number 65 is assigned the letter A. The C++ expression that you'll need to get the letter from an integer i is:
static_cast(i)
This is an example of a type cast as described in your textbook.

In your program, you should print only these letters (no endls). Run the program with the input file message.dat that you downloaded earlier.