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


Drill 9-4

Write a program that puts all the file tools you have learned together in one menu, using the payroll application. The menu should contain the following options:

  Display employee file.
  Display an employee record.
  Add a new employee.
  Exit

Appending a File

If you would like to add the information for a new employee to the file EMPFILE.BIN, you cannot run the program 9-8 again because it will erase the whole file. There is another way to do this.

Adding data to an existing file is called appending, as the new data are written to the end of a sequential file. In some implementations (including Turbo) the file can be opened for appending using the procedure APPEND, which takes the form:

APPEND(file-variable)

While the REWRITE procedure positions the file pointer at the beginning of the file, APPEND positions the file pointer at the end of the file, so any new data will be written there. If your implementation does not have the procedure APPEND you need to use the following technique to add new items to the file:

1.  Open the file EMPFILE.BIN for reading using RESET.
2.  Open a scratch file (e.g., NEWFILE.TMP) for writing using REWRITE.
3.  Copy each item from EMPFILE.BIN to NEWFILE.TMP, then accept the new data from the keyboard and write them to NEWFILE.TMP.
4.  Open NEWFILE.TMP for reading and EMPFILE.BIN for writing, then copy the contents of NEWFILE.TMP back to EMPFILE.BIN.
5.  Erase the scratch file NEWFILE.TMP.

In standard Pascal, if the file variable is not included in the program header, the file is considered a temporary file and will be erased right after the program execution. You get the same result in UCSD if you close the file using the keyword PURGE.

In Turbo you can erase a file after closing it by using the procedure ERASE, which takes the form:

ERASE(file-variable);

If the information in the file needs to be changed (as in the case of a salary increase for employees), you can use a similar algorithm to update a sequential file. With random access files you can easily add or update records, but this kind of file will not be covered in our three days.

The following “Menu” procedure may be used in this program:

{ --------------- Procedure Menu ------------------ }
PROCEDURE Menu;
VAR
  Option :INTEGER;
BEGIN
  WRITELN(Header);
  WRITELN;
  WRITELN('1. Display employee file.');
  WRITELN('2. Display an employee record.');
  WRITELN('3. Add a new employee.');
  WRITELN('4. Exit.');
  WRITELN(Separator);
  WRITE('Make a choice and press a number: ');
  READLN(Option);
  CASE Option OF
    1: Readit(DbFile);
    2: ReadRec(DbFile, EmployeeRec);
    3: AddRec(NewFile, DbFile, EmployeeRec);
    4: Exit
  END;
  Menu
END;

As you can see in the “Menu” procedure, the options “1” to “3” correspond to the procedures you have to design. For the fourth option you may use the Turbo Pascal procedure (EXIT), a GOTO, or any suitable statement in your compiler that lets you exit from the repeated menu. Notice also that in this example a scratch file “NewFile” was used for adding a new employee to the file (option #3), but if you have the procedure APPEND in your compiler, you should use it instead, as it will save you a lot of effort. This program is the nucleus of a database and can be modified to include more features, such as updating employees’ information and removing unwanted records from the database.

Summary

In this chapter you learned the main tools for handling data files:

1.  You know that standard Pascal provided TEXT and non-TEXT sequential files, while modern versions also provide random/direct access files.
2.  During your tour of sequential files, you learned how to declare, create, write to, and read from a file.
3.  TEXT files are declared using the form:
file-variable :TEXT;
4.  Files of other types are declared using the forms:
TYPE
type-identifier = FILE OF component-type;
VAR
file-variable : type-identifier;

In standard Pascal the file variable must be included as one of the file parameters or the file will be considered a temporary one and automatically deleted after the execution. In UCSD you must use the PURGE keyword to delete such a temporary file, and in Turbo the ERASE procedure.
5.  The procedures used to open a file for either input or output are:
RESET( file-variable); (for input)
REWRITE( file-variable); (for output)

With modern versions of Pascal (such as Turbo) you can also open a sequential file for appending with the procedure APPEND, which has a similar form to those above.
6.  With versions other than standard Pascal, the file variable must be linked to the actual file name on the disk. In Turbo this is done by using the procedure ASSIGN; in UCSD the actual file name comes as a second parameter of the RESET or REWRITE procedures. In these implementations the file must be closed after processing using the CLOSE procedure.
7.  You learned the general form of the input/output statements:
 
READLN( file-variable, input-list);
READ( file-variable, input-list);
WRITELN( file-variable, output-list);
WRITE( file-variable, output-list);

and you know that the READLN and WRITELN procedures may not be used with non-TEXT files.
8.  You also learned the general form of EOF and EOLN functions:
EOF( file-variable)
EOLN( file-variable)

Finally, you have had enough practice to enable you to create and manipulate files for different applications.


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.