Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Chapter 9
Files and Applications

9-1 Data Files

In the previous chapters you have used different data structures in which to store data items and you know how to organize your data for optimum processing efficiency. If you do not store data to the disk, however, every data item you entered into a program will evaporate when the program exits. Using disks to store your data in files will enable you to save your data permanently and retrieve them later for either reviewing or further processing.

A FILE (which is a structured type) is in general defined as a collection of related items stored on disk or any other external storage medium and arranged in sequence as shown in the following figure.

item-1 item-2 item-3 item-4 ... EOF

A file item (also called a file component or file element) may be of any simple or structured type except the type FILE.

Files may be accessed to perform either one of the following operations:

  Reading from a file (input)
  Writing to a file (output)

Files can be organized as either sequential access files or random (direct) access files. In the first method any item in the file cannot be reached unless all the preceding items are read. One example of a sequential access file is a purchase list which has to be read from the top down to access a specific item. The random access file is organized like a set of post office boxes, which are identified by numbers and accessed directly without the need to go through them all.

While standard Pascal allows only sequential access files, many implementations of Pascal (including Turbo and UCSD) provide random access files as well. The files discussed in this chapter are all sequential access files.

9-2 Text Files

Standard Pascal provides two types of files, TEXT files and non-TEXT files (also called binary or typed files).

A TEXT file is the simpler file structure as its elements are all characters (of the type CHAR). You have already used the standard INPUT file (the keyboard) and the standard OUTPUT file (the screen), which are classified as TEXT files. A TEXT file consists of successive lines of characters separated by end-of-line marks and ends with the end-of-file mark, as in this example:

This is a text file. (EOLN)
Each line is composed of successive characters. (EOLN)
Lines are separated by end-of-line marks. (EOLN)
The file is terminated by an end-of-file mark. (EOLN)
(EOF)

The file on the disk looks exactly the same as the file you type onto the screen from the keyboard. The characters in a TEXT file are stored in the ASCII format (or EBCDIC in some systems), which means that if the file contains a number like “1234” it will be stored in four bytes, each byte representing the ASCII code of a digit. This is not the case if the number is treated as an INTEGER, in which case it is stored in the internal binary format (0000010011010010) in two bytes.

9-3 Reading a Text File

In Chapter 8 you used program 8-1 to read a TEXT file from the keyboard character by character and categorize each character in the file. In this section, the same logic will be used to read and analyze a previously stored text file on the disk. You need to make a few changes in program 8-1 to make it read a disk file. As we discuss these changes, you will learn the protocols necessary to retrieve information from a TEXT file.

The File Variable

In order to use a disk TEXT file you have to declare a file variable of the type TEXT. If you choose a name like “DiskFile” for this purpose, the declaration will be:

    VAR
     DiskFile:TEXT;

File Parameters

To use a file in standard Pascal, you must include the file variable (“DiskFile” in our example) among the other file parameters in the program header (this is not necessary in other implementations).

    PROGRAM TextAnalyzer2(OUTPUT, DiskFile);

Here, the parameter INPUT is not needed as long as no data are to be read from the keyboard. The parameter OUTPUT, however, is necessary for displaying the output.

Opening a File for Input: RESET

To read a text file, it must be opened using the standard procedure RESET as follows:

    RESET(DiskFile);

The parameter of the procedure is the file variable.

In Turbo Pascal another procedure ASSIGN must also be used to link the actual data file on the disk to the file variable. If the text file to be read is the file “C:\\CONFIG.SYS” (which already exists in the root directory of the hard disk “C:”), then the following statement must be used before opening the file:

    ASSIGN(DiskFile, 'C:\\CONFIG.SYS');

You may replace the file “CONFIG.SYS” with any other existing file, or you can write a new text file with any text editor (such as EDIT or EDLIN). In any case, if the file is not in the current directory or on the current disk, you have to include the complete PATHNAME of the file as shown in the statement above (for more details on PATHNAME refer to your DOS manual).

Because Turbo Pascal was used to compile the programs in this book, the following two statements were used to open the file:

    ASSIGN(DiskFile, 'C:\\CONFIG.SYS');
    RESET(DiskFile);

Now the file “config.sys” is ready for input, and the file pointer is pointing to the first item (character) in the file.

Some implementations (such as UCSD) link the file variable and the file name with the RESET procedure, thus making the ASSIGN procedure unnecessary:

    RESET(file-variable, file-name);


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.