|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
Appending a FileIf 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:
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:
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:
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. SummaryIn this chapter you learned the main tools for handling data files:
Finally, you have had enough practice to enable you to create and manipulate files for different applications.
|
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. |