|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
9-6 Non-Text FilesThe TEXT file is a special predefined type of file, but as mentioned earlier the general definition of a file allows the file components to be of any type other than the type FILE. You can declare a file of any predefined or user-defined type using the form:
The component type can be a simple type (like INTEGER), a structured type (like an array), or a user-defined type (like a record). The following is an example of a file declaration whose components are records (a simplified form of EmployeeRecord is used to make the program shorter): TYPE EmployeeRecord = RECORD ID :INTEGER; Name :STRING[20]; Rate :REAL; END; EmpFileRec = FILE OF EmployeeRecord; VAR F :EmpFileRec; { The file variable } EmployeeRec:EmployeeRecord; { The record variable } The main properties of non-TEXT files are:
Application 4: Payroll SystemThis is the same payroll program but in a better shape. The program is divided into two separate modules (programs). The first module (figure 9-8) reads the employees records from the keyboard and stores them in a non-TEXT file EMPFILE.BIN. In the second module (figure 9-9) the HoursWorked are entered from the keyboard and wages are calculated and written to the file PAYFILE.TXT, which is a TEXT file. The first program may be used only once to create the employee file, but the second program is used every pay period to create the PayFile. Here is the first program: { ------------------------------ figure 9-8 ------------------------------ } PROGRAM EmpPayInfo(INPUT,OUTPUT,F); { This program is used to create a user-defined file EMPFILE.BIN whose components are records. } TYPE EmployeeRecord = RECORD ID :INTEGER; Name :STRING[20]; Rate :REAL; END; EmpFileRec = FILE OF EmployeeRecord; VAR F :EmpFileRec; { The file variable } EmployeeRec:EmployeeRecord; { ----------- Procedure WriteRecord ---------- } PROCEDURE WriteRecord; BEGIN { Store one record in the file } WRITE(F, EmployeeRec) END; { ----------- Procedure GetData ---------- } PROCEDURE getdata; VAR Counter:INTEGER; BEGIN Counter:= 0; WITH EmployeeRec DO BEGIN WRITE('Please enter Employee ID (or 0 to end):'); READLN(ID); WHILE ID <> 0 DO BEGIN Counter:= counter + 1; WRITE('Employee Name: '); READLN(Name); WRITE('Hourly Rate: '); READLN(Rate); WriteRecord; WRITE('Please enter Employee ID (or 0 to end):'); READLN(ID) END END; WRITELN(Counter, ' Employee records have been filed.') END; { ---------------- Main Program -------------- } { Main Program } BEGIN ASSIGN(F, 'EMPFILE.BIN'); REWRITE(F); GetData; CLOSE(F); WRITELN('Press ENTER to continue..'); READLN END. The second module (PayRoll2) is made up of four procedures:
{ ------------------------------ figure 9-9 ------------------------------ } PROGRAM PayRoll2(INPUT,OUTPUT,MasterFile,PayFile); { This program reads the file EMPFILE.BIN one record at a time, then calculates wages and stores the output in the text file PAYFILE.TXT } TYPE EmployeeRecord = RECORD ID :INTEGER; Name:STRING[20]; Rate:REAL; END; PayRecord = RECORD ID :INTEGER; Name :STRING[20]; Wages :REAL; END; EmployeeFile = FILE OF EmployeeRecord; VAR MasterFile :EmployeeFile; PayFile :TEXT; EmployeeRec :EmployeeRecord; PayRec :PayRecord; HoursWorked, Wages :REAL; { --------------- Procedure GetInfo ------------------ } { This procedure reads and displays a record from the file EMPFILE.BIN } PROCEDURE GetInfo(VAR F:EmployeeFile); BEGIN READ(F,EmployeeRec); WITH EmployeeRec DO BEGIN WRITELN('ID: ',ID); WRITELN('Name: ',Name); WRITELN('Hourly rate: $', Rate:0:2); END; END; { --------------- Procedure CalcWages ---------------- } PROCEDURE CalcWages(HoursWorked:REAL; VAR Wages:REAL); BEGIN WITH EmployeeRec DO Wages := Hoursworked * Rate; Wages := ROUND(100 * Wages) / 100 { rounding cents } END; { -------------- Procedure FilePayRoll --------------- } { This procedure writes a record to PAYFILE.TXT } PROCEDURE FilePayRoll(VAR F:EmployeeFile; VAR P:TEXT; Wages:REAL); BEGIN WITH EmployeeRec DO BEGIN PayRec.ID:= ID; PayRec.Name:= Name; PayRec.Wages:= Wages END; WITH PayRec DO BEGIN WRITELN(P, ID); WRITELN(P, Name); WRITELN(P, Wages); end; END; { -------------- Procedure ReadPayRoll --------------- } { This procedure reads and displays PAYFILE.TXT } PROCEDURE ReadPayRoll(VAR P:TEXT); VAR I :INTEGER; BEGIN WITH PayRec DO BEGIN READLN(P, ID); READLN(P, Name); READLN(P, Wages); WRITE(ID:3,' '); WRITE(Name); { Fill the rest of the 20 places with blanks } FOR I:= 1 TO 20-LENGTH(Name) DO WRITE(' '); WRITELN(' $',Wages:0:2) END; END; { --------------- Main Program ------------------ } BEGIN ASSIGN(MasterFile, 'EMPFILE.BIN'); RESET(MasterFile); ASSIGN(Payfile, 'PAYFILE.TXT'); REWRITE(PayFile); WHILE NOT EOF(MasterFile) DO BEGIN GetInfo(MasterFile); WRITE('Please enter hours worked for this pay period: '); READLN(HoursWorked); CalcWages(HoursWorked, Wages); FilePayRoll(MasterFile, PayFile, Wages) END; CLOSE(MasterFile); CLOSE(PayFile); RESET(PayFile); WRITELN('--------- PayRoll Summary --------- '); WRITELN('ID # ------- Name -------- Salary'); WHILE NOT EOF(PayFile) DO ReadPayroll(PayFile); WRITELN('----------------------------------- '); CLOSE(PayFile); WRITELN('Press ENTER to continue..'); READLN END.
|
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. |