PASCAL Programming: § 5: Abstract Data Structures and Files

Instructor: M.S. Schmalz

Computer program design can be made much easier by organizing information into abstract data structures (ADS). For example, one can model a table that has three columns and an indeterminate number of rows, in terms of an array with two dimensions: (1) a large number of rows, and (2) three columns. A key feature of modern computer programs is the ability to manipulate ADS using procedures or methods that are predefined by the programmer or software designer. This requires that data structures be specified carefully, with forethought, and in detail. This section is organized as follows:

In this introductory class, we will concentrate only on the data structures called arrays, which are discussed in Section 5.1. We then progress to a discussion of how PASCAL handles information stored in DOS files (Section 5.2), and review in detail the PASCAL commands for file input/output (I/O, Section 5.3).The section on file I/O will be updated periodically to reflect updates in Turbo PASCAL syntax and usage.

5.1. Arrays and their Manipulation.

We begin with several observations about the use of arrays in computer programs.

A key problem with arrays is that they have fixed size. Hence, they are called static data structures. In a more advanced class, we would examine techniques for programming data structures called lists, which can expand and contract with the data that one puts into or takes out of the list. Another problem of arrays which we mentioned previously is that they are statically typed, i.e., cannot be used to store any type of data, but only the type of data assigned to them in the VAR statement in which they were declared. It is interesting to note that certain languages, such as SNOBOL and ICON, have circumvented this difficulty by providing a TABLE data structure that can hold data of any type. You are not responsible for knowing about the TABLE data structure, however, since it is not available in PASCAL.

5.2. DOS Files and Turbo PASCAL.

Turbo PASCAL supports many different types of file operations through its DOS interface, which is transparent to the user. In Section 2, we mentioned file operations such as open, read, write, and close, which we now discuss in some detail.

In order to understand file structures, it helps to think of a disk drive in terms of a drawer in a filing cabinet. In each drawer, there are many files, which are usually contained in manila folders. In order to view, create, or modify the contents of a folder, one must first retrieve then open the folder. This is similar to initializing and opening a computer disk file.

If one wants to view the file contents, then one must read the file, which holds for either physical or computer files. Similarly, creating new file contents or modifying existing file information is accomplished by writing to the file.

After one has completed operations on a given file, it is returned to the file cabinet, to keep the work area neat (this helps one find the file when it is next needed). A similar situation holds for computer disk files, where one closes the file in order to deallocate file pointers assigned by the file I/O library and runtime module.

Additional file operations, such as changing file read/write permissions are also possible. However, these are within the purview of more advanced topics, and are not part of the basic file operations reviewed in these class notes.

5.3. PASCAL File I/O Commands.

The PASCAL language provides constructs for allocating or initializing, opening, reading, writing, and closing files. File addresses or references are expressed in terms of symbolic file handles, which are represented in PASCAL as names assigned to a given file. The following commands pertain:

ASSIGN statement:

There are two types of file opening statements, one of which opens the file for reading (input), the other for writing (output).

RESET statement:

REWRITE statement:

PASCAL has two ways of reading or writing to a file. The READLN (or WRITELN) statement reads a sequence of characters terminated by a newline character or a carriage return, whereas the READ (WRITE) statement reads the file as a stream of characters. Since we have already covered the READLN and WRITELN statements in class, which we used to obtain input from (send output to) the computer keyboard (monitor), we herein discuss the READ and WRITE statements only. The syntax of READLN and WRITELN is symmetric to that of the READ and WRITE statements.

READ statement:

WRITE statement:

CLOSE statement:

This concludes our summary of PASCAL file commands. Details of command usage, options, formatting, etc. will be given in class.

Copyright © 1997 by Mark S. Schmalz
All rights reserved, except for viewing/printing by UF faculty or students registered for this class.