|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
10-3 Deleting Records from the FileThe algorithm to delete an employee record from the payroll file is as follows:
To do that you need to add the following procedure to the program. (The source code of this procedure is on the companion disk under the name del-proc.pas.) { ------------------------- Procedure DelRec ------------------------- } PROCEDURE DelRec(VAR NewFile, PayrollFile:TEXT; Employee:EmployeeRecord); VAR SSNumber:STRING[11]; BEGIN ASSIGN(PayrollFile, FileName); RESET(PayrollFile); ASSIGN(NewFile, TempFile); REWRITE(NewFile); WRITE('Please enter the SSN of the employee to be deleted: '); READLN(SSNumber); WHILE NOT EOF(PayrollFile) DO BEGIN WITH Employee DO BEGIN READLN(PayrollFile, ID); READLN(PayrollFile, Name); READLN(PayrollFile, Position); READLN(PayrollFile, SSN); READLN(PayrollFile, Category); CASE Category OF '1': READLN(PayrollFile, MonthlySalary); '2': READLN(PayrollFile, HourlyRate); '3': BEGIN READLN(PayrollFile, Commission); READLN(PayrollFile, BasicSalary); READLN(PayrollFile, Area) END END; { End of CASE structure } IF SSNumber <> SSN THEN BEGIN WRITELN(NewFile, ID); WRITELN(NewFile, Name); WRITELN(NewFile, Position); WRITELN(NewFile, SSN); WRITELN(NewFile, Category); CASE Category OF '1': WRITELN(NewFile, MonthlySalary:0:2); '2': WRITELN(NewFile, HourlyRate:0:2); '3': BEGIN WRITELN(NewFile, Commission:0:2); WRITELN(NewFile, BasicSalary:0:2); WRITELN(NewFile, Area) END END; { End of CASE structure } END END { End of WITH block } END; CLOSE(NewFile); CLOSE(PayrollFile); { Copy NewFile back to Payroll File } ASSIGN(PayrollFile, FileName); REWRITE(PayrollFile); ASSIGN(NewFile, TempFile); RESET(NewFile); WHILE NOT EOF(NewFile) DO BEGIN READLN(NewFile, OneLine); WRITELN(PayrollFile, OneLine) END; CLOSE(NewFile); ERASE(NewFile); { Erase the temporary file } CLOSE(PayrollFile); WRITELN('The employee ', SSNumber, ' is removed from file.') END; In order to have the Delete Record option as one of the menu items, you need to modify the Menu procedure. It may look similar to the procedure below (this procedure is on the companion disk under the name mnu-proc.pas). { ------------------------- Procedure Menu --------------------------- } PROCEDURE Menu; VAR Option:INTEGER; BEGIN WRITELN(Header); WRITELN; WRITELN('1. Display an employee record.'); WRITELN('2. Add a new employee.'); WRITELN('3. Delete an employee.'); WRITELN('4. Exit.'); WRITELN(Separator); WRITE('Make a choice and press a number: '); READLN(Option); CASE Option OF 1: ReadRec(PayrollFile, EmployeeRec); 2: AddRec(NewFile, PayrollFile, EmployeeRec); 3: DelRec(NewFile, PayrollFile, EmployeeRec); 4: Exit END; Menu END; Sample run: The following is a sample run to delete the record of the employee whose SSN is 347-12-3456. The user input is italicized and bolded for clarity. ------------- Main Menu -------------- 1. Display an employee record. 2. Add a new employee. 3. Delete an employee. 4. Exit. -------------------------------------- Make a choice and press a number: 3 Please enter the SSN of the employee to be deleted: 347-12-3456 The employee 347-12-3456 is removed from file. ------------- Main Menu -------------- 1. Display an employee record. 2. Add a new employee. 3. Delete an employee. 4. Exit. -------------------------------------- Make a choice and press a number: 4
|
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. |