Testing Assignment 8: I/O Redirection

October 1996


Assignment 6 (the first rock-shipping program) was graded for programming problems, but we didn't actually check to be sure everyone had the right equations. In this assignment, you'll have to give us a printout of the program's output, so we can make sure the numbers come out right. (You should check it yourself first; compare your output on the original program, using the 3.5 pound rocks, to the output shown on Assignment 6.)

Of course, this is an interactive program, so it doesn't usually produce a printout. To make one, you'll use a feature of MS-DOS called "input/output redirection". Remember that DOS treats the keyboard and screen as files. What we'll do is tell DOS to use an actual disk file for input, instead of the keyboard. (This is also explained in Savitch, pp.A-38.)

Step 1: Get the Data

Here's what to do. First, in Netscape, click on the link just below this paragraph. When the file appears (it's just a long list of numbers and letters), use Netscape's FILE/SAVE-AS menu to save the file on your PC's c: drive. Save it under the same name it has here, INPUT8.TXT. Here's the link:

NOTE: If the PC you're working on doesn't have a C: drive, use the D: drive for this step and the steps below. (If you have problems with this step, you could just use the Turbo editor to create a text file that includes exactly the list of numbers and letters you see in Netscape.)

Step 2: Modify your program to stop on rocks = -1111

If you're using a truly infinite loop (while true...) for your program, you'll have to change it so the program stops whenever a value of -1111 is entered for the number of rocks. That's because MS-DOS will be able to type in numbers and letters as it runs your program, but it can't press Ctrl-C to make it stop.

Step 3: Save your program as a stand-alone application

When your program is ready, pull down Turbo Pascal's COMPILE menu and select the DESTINATION MEMORY option. That's a "toggle", which means that when you select it, it changes to DESTINATION DISK. (Of course, you have to pull down the menu again to see this -- not a great interface design.)

With the menu item set to DESTINATION DISK, compile your program. Turbo Pascal will save the compiled version as a new file. If your program's source code is called ROCKS.PAS, then the new file will be called ROCKS.EXE.

The ROCKS.EXE file is a "stand-alone" program, just like MS Word, or Netscape, or any other application. You can run ROCKS.EXE without owning the programming environment (Turbo) that created it. You could copy the file to a floppy disk and give it to someone to run on their computer.

To run ROCKS.EXE, get into the DOS Shell (use Turbo's FILE menu), and type ROCKS. The program should run just like it did when you selected RUN (or Ctrl-F9) in Turbo.

Step 4: Let DOS run the program

Now use i/o redirection, as follows: First, make sure that INPUT8.TXT is in the directory you're working in, along with ROCKS.EXE. Then, type the followiing command at the DOS prompt:

      c:> rocks < input8.txt

If everything has worked out correctly, your program will run, it will send its prompts to the screen, and the prompts will be answered by the numbers and letters in INPUT8.TXT. It will look a little weird, since you can't see what MS-DOS is typing -- not even the Enter keys (carriage returns). It should look about like this:

     Rocks: Type: Zone: Regular shipping charges: $ 17.50
     Express shipping charges: $ 61.90

     Rocks: Type: Zone: Regular shipping charges: $ 92.50
     Express shipping charges: $283.90

The program should stop on its own, because INPUT8's last entry for the number of rocks is -1111. (If the program keeps running, type Ctrl-C. That could mean the transfer of INPUT8.TXT from Netscape to DOS didn't work quite right. You might have to use an editor to recreate the file. First, though, try just reading the file into the Turbo editor and then saving it again, without any change -- that may fix things.)

Note that the INPUT8 file includes a couple of things your program should be able to handle "gracefully" -- upper and lower case letters for the rock type, and one example of an unlisted rock type. The program shouldn't crash on that, but it should give some sort of simple warning.

Step 5: Let DOS record the output

OK, last step. You've got the program to take its input from a file, now you want it to send its output to a file. Type this line:

     c:> rocks < input8.txt > output8.txt

The program should run, but since all the output is going to a file, you won't see anything. The DOS prompt should come right back. Now at the DOS prompt, type:

     c:> type output8.txt

You should see the contents of OUTPUT8.TXT, which is the output produced by ROCKS for the input from INPUT8.TXT. Print out OUTPUT8.TXT and hand that in with the printout of your final program (and the graded assignment 6).


RETURN to CSCI 1200 Home Page

- John Rieman