Unit 1.5 Input and Output Files

PROG

Reads two numbers and produces the sums.

PED

To learn how to make your program read input files and write output files.

CONCEPTS

Include file_name:text; after the var statement for each file you wish to use.

for input files, after the begin include

assign(file_name,'dos_name');
reset(file_name);

assign(file_name,'dos_name);
rewrite(file_name);

Each read or write to a file will have the file_name in parentheses.

to go to the next line of the output file, we have to:

writeln(file_name);

also for each output file, write

close(file_name);

SF

file_name:text; creates a text_file variable which can be associated with a file on the operating system
put after line with "var"

assign(file_name,'dos_name'); associates a file given by the operating system with a text_file variable defined above

dos_name is of the form (up to eight characters).(three characters)
the last three characters are called the extension

reset(text_file_name); -- gets the file ready to input
rewrite(text_file_name); -- gets the file ready to output

read(text_file_name,box_name); -- copies the next number out of text_file_name and puts the number into box_name

write(text_file_name,box_name); -- copies the contents of the box_name to the end of the text_file_name;

writeln(text_file_name) -- puts a carriage return on text_file_name so the next number will appear on next screen.

close(text_file_name); -- makes sure no output gets lost.



Unit 1.5 Input and Output Files

Click here to view Unit 1.5 Pascal program.

All the programs we have discussed so far take input from the screen and generate output to the screen--albeit sometimes with niceprompts.

Sometimes, it is desired to keep the data on the files. Otherwise, if we had 100 numbers to enter into a program, we would have to enter all 100 numbers correctly. This is a pain. An example of a file is the PASCAL program itself which is entered in the computer is a file which is input to a program called the compiler. If we had a single error, it would be a pain to retype the program.

We create the input files with the editor just like we create PASCAL programs.

It is also nice to have an output file from time to time. We can print the output file out. Or we could use the output file as input to another program--either one we have written or another program.

We can visualize a file as being a piece of paper on which the editor can insert numbers. When the program runs, each read statement causes the transfer of a single number from the file into a box_name. As each read statement executes and transfers a number, we can visualize that the number is crossed off. The next read statement will retrieve the number that follows. (Of course, if we should restart the program all the cross-marks are removed and we will start reading from the first line. That is what the word "reset" means in the program.)

In the case of output files, each write statement will append a new number to the end of the piece of paper. IF we rerun the program, whatever data was on the piece of paper would be erased and we would start on a clean piece of paper.

The operating system has names for electronic pieces of paper, called a dos_name. This would be the name one would specify in the PRINT command to see our output or to the editor when we create an input file.

The PASCAL has a different name for the piece of paper, called a text_file_name.

The text_file_name is defined right after the var statement with our list of box names. In fact both box_names and text_file_names are identifiers, the first contains numbers and the second refers to a file. We write text_file_name:text; for each one we have. In our example, we see two text_file_names on lines 3 and 4, input_file and output_file.

To associate the text_file_name with a dos_name, i.e., an actual electronic piece of paper, we use the assign statement. This is of the form assign(text_file_name,dos_name); The dos name appears in quotes; actually it is a character string constant. We follow the assign statement by either a reset statement if it is input or a rewrite statement if it is output. They take a single argument, a text_file name. Lines 6 and 7 take care of our input file, associating input_file with 'input.txt' and then setting the file for input on line 7. Likewise, lines 8 and 9 associate output_file with output.txt.
The rewrite statement will cause us to start writing at the beginning of the file.

Remember, our program is supposed to read two numbers from our input file and put the result in the output_file. Line 10 and 11, read the two numbers to be summed into a and b. Notice in the parentheses, we write "input_file."

Line 12 does the actual summing. Line 13 puts the sum onto the output_file, output.txt. And line 14 appends an extra blank line. We also have to close the output file so as to ensure that we can see all the output data. Note line 15.

By the way, it is possible to intermix read and writes without text_file_names
with read and writes that do have them. The latter would get data to and from the keyboard. For example, we might want to read a number specifying a report type from the user and then prepare a file containing the indicated report in a file for the user to print out.