Programming-in-PASCAL

Document Sample
Programming-in-PASCAL Powered By Docstoc
					Programming in Pascal - 9

Programming in PASCAL
Contents Introduction Record Position Pascal SEEK procedure An Inventory System

Module 09 – Files (Random)

Introduction
In the previous module we considered the processing of serial and sequential files. The main disadvantage of this type of file organisation is that whenever we need to look at a record the program has to search the file from the beginning to locate it. To update a record the whole file has to be copied. A record can be added at the end of the file if it is a serial file while for a sequential file the file is copied with the new record being inserted at the appropriate place during the copying so that the order of the file is maintained. This process is often referred to as „updating by copying‟. Serial and sequential file organisation works best when the same operation is being carried out on most or all of the records. Random or direct file organisation is most useful when dealing with individual records. A record is accessed in some way by relating its position in the file to its primary key field. The record pointer is moved to the position of the record and the record is read. Because records are usually of a fixed size the record can be amended and then written back to its original position. This process is often referred to as „updating in-situ‟.

Record Position
The simplest way of determining the position of a record in a random file is to have a primary key field that is an integer which will also be the record position or address in the file. Thus in the worked example below an inventory file contains item records where the record address is given by the item code. So if item code is 25 then the record position or address is 25. This works well when the primary key is in the range 0 to the maximum number of records the file can hold. Where the primary key does not map conveniently to record address then a hashing algorithm is used to convert the primary key into an allowed address in the file. This makes record processing more complicated as records having the same address have to be allowed for. For more information about hashing algorithms see page 121 of “A level Computing”, 4th Edition by P.M. Heathcote.

Pascal SEEK procedure
The SEEK procedure gives Pascal the ability to directly access a record in a file. It takes the form: seek(FileVariable,RecordAddress);

9.1

Programming in Pascal - 9

If record 25 is to be read then the code would something like: //Read record 25 seek(InventoryFile,25); read(InventoryFile,OneItemRecord); The seek command moves the record pointer to the record with address 25 and the read command reads the data into the record variable OneItemRecord. It is important to note that when a read takes place the record pointer is automatically moved to the next address (26). Thus if the record was to be updated a second seek would be required to put the record pointer back to the correct address. This would be followed by a write command: //Write/Update record 25 seek(InventoryFile,25); write(InventoryFile,OneItemRecord);

An Inventory System
The worked example below shows a simple computer hardware inventory system. The first program creates the data file containing 500 blank or dummy records. A field called ItemActive is part of the item record and is used to indicate whether or not an item exists or not. Thus when adding an item this field is set to true and when an item is deleted it is set to false. 1. Inventory file creation
// Inventory file initialisation program inventoryinit; uses SysUtils, win32crt; const InventoryFilePath ='inventory.dat' ; MaxRecords = 500; Yes = True; No = False; type ItemIDType = integer; ItemNameType = string[20]; ItemTypeType = string[15]; ItemDescriptionType = string[30]; ItemSerialType = string[20]; ItemSupplierType = string[25]; ItemCostType = real; ItemRecordType=record ItemID : ItemIDType; ItemName : ItemNameType; ItemType : ItemTypeType; ItemDescription : ItemDescriptionType; ItemSerial : ItemSerialType; ItemSupplier : ItemSupplierType; ItemCost : ItemCostType; ItemActive : Boolean; end;

9.2

Programming in Pascal - 9

var MyInventoryFile : file of ItemRecordType; procedure createinventoryfile; //Creates and opens the inventory file begin assign(MyInventoryFile, InventoryFilePath); rewrite(MyInventoryFile); end; procedure closeinventoryfile; //Closes the inventory file begin close(MyInventoryFile); end; procedure writeoneRecord(AID:integer; ARecord:ItemRecordType); //Writes one record at specified address begin seek(MyInventoryFile, AID); write(MyInventoryFile,ARecord); end; procedure writeoneDummyRecord(AID:integer; ARecord:ItemRecordType); //Initialises the dummy record data and then writes in to file //by calling the writeonerecord procedure begin with ARecord do begin ItemID:=AID; ItemName :=''; ItemType :=''; ItemDescription :=''; ItemSerial :=''; ItemSupplier :=''; ItemCost :=0; ItemActive :=No; end; writeoneRecord(AID,ARecord); end; procedure initInventoryFile(Max : integer); //Creates file and then fills it with Max dummy records var count : integer; MyInventoryRecord : ItemRecordType; begin createinventoryfile; for count:=1 to Max do begin with MyInventoryRecord do begin ItemId:=count; writeoneDummyRecord(ItemId, MyInventoryRecord); write('.'); end; end; closeinventoryfile; end;

9.3

Programming in Pascal - 9

//Main program begin clrscr; writeln('Creating Inventory File'); writeln('Writing ',MaxRecords, ' records.'); initInventoryFile(MaxRecords); writeln; writeln('Finished.'); end.

2. Inventory file maintenance
program inventorymaint; uses SysUtils,win32crt; const InventoryFilePath ='inventory.dat' ; MaxRecords = 500; Yes = True; No = False; type ItemIDType = integer; ItemNameType = string[20]; ItemTypeType = string[15]; ItemDescriptionType = string[30]; ItemSerialType = string[20]; ItemSupplierType = string[25]; ItemCostType = real; ItemRecordType=record ItemID : ItemIDType; ItemName : ItemNameType; ItemType : ItemTypeType; ItemDescription : ItemDescriptionType; ItemSerial : ItemSerialType; ItemSupplier : ItemSupplierType; ItemCost : ItemCostType; ItemActive : Boolean; end; var MyInventoryFile : file of ItemRecordType; procedure continue(x,y:integer); //A utility routine to pause output //The prompt is placed at coordinates x,y var key:char; begin gotoxy(x,y); write('Press a key to continue..'); key:=readkey; end;

9.4

Programming in Pascal - 9

procedure openinventoryfile; begin assign(MyInventoryFile, InventoryFilePath); reset(MyInventoryFile); end; procedure closeinventoryfile; begin close(MyInventoryFile); end; procedure writeoneRecord(AID:integer; ARecord:ItemRecordType); begin seek(MyInventoryFile, AID); write(MyInventoryFile,ARecord); end; procedure getOneRecord(AID:integer;var ARecord:ItemRecordType ); //Read a specified record begin seek(MyInventoryFile,AID); read(MyInventoryFile,ARecord); end; function getActiveRecord(AID:integer): Boolean; //Locates a given record and returns true if it is active else false var OneItemRecord : ItemRecordType; begin getOneRecord(AID,OneItemRecord); if oneItemRecord.ItemActive then getActiveRecord:=Yes else getActiveRecord:=No; end; function getItemCode:integer; //Simple function that returns a valid item code from keyboard input var ACode:integer; begin clrscr; write('Please enter an item code: '); readln(ACode); while (ACode<0) or (ACode>MaxRecords) do begin write('Please enter an item code (0..',MaxRecords,'): '); readln(ACode); end; getItemCode:=ACode; end; procedure AddOneRecord; //Adds new item record to file //Checks to see if item already active/exists //No real data validation included yet var ACode:integer; oneItemRecord:ItemRecordType; begin openinventoryfile; ACode:=getItemCode; If getActiveRecord(ACode) then begin writeln('Item code already in use.')

9.5

Programming in Pascal - 9

end else clrscr; with oneItemRecord do begin ItemId:=ACode; //Display prompts writeln('Enter new record details: '); gotoxy(2,4);write('Item ID : ',ItemID); gotoxy(2,5);write('Item Name : '); gotoxy(2,6);write('Item Type : '); gotoxy(2,7);write('Item Description : '); gotoxy(2,8);write('Item Serial No : '); gotoxy(2,9);write('Item Supplier : '); gotoxy(2,10);write('Item Cost : '); //Get input gotoxy(25,5);readln(ItemName); gotoxy(25,6);readln(ItemType); gotoxy(25,7);readln(ItemDescription); gotoxy(25,8);readln(ItemSerial); gotoxy(25,9);readln(ItemSupplier); gotoxy(25,10);readln(ItemCost); //Set the record to active then write the record ItemActive:=True; writeoneRecord(ItemID,oneItemRecord); end; closeinventoryfile; end; procedure ShowRecordDetails(ARecord:ItemRecordType); begin with ARecord do begin clrscr; gotoxy(2,4);write('Item ID : ',ItemID); gotoxy(2,5);write('Item Name : ',ItemName); gotoxy(2,6);write('Item Type : ',ItemType); gotoxy(2,7);write('Item Description : ',ItemDescription); gotoxy(2,8);write('Item Serial No : ',ItemSerial); gotoxy(2,9);write('Item Supplier : ',ItemSupplier); gotoxy(2,10);write('Item Cost : ',ItemCost:8:2); continue(26,20); end end; procedure ShowOneRecord; //Read a specified record then display it var ACode : integer; OneItemRecord : ItemRecordType; begin openinventoryfile; clrscr; ACode:=getItemCode; if getActiveRecord(ACode) then begin getOneRecord(Acode,OneItemRecord); ShowRecordDetails(OneItemRecord); end else begin writeln('This item does not exist!'); continue(26,20); end; closeinventoryfile; end;

9.6

Programming in Pascal - 9

procedure ListActiveRecords; //Display all active records in file var ACode : integer; OneItemRecord : ItemRecordType; begin openinventoryfile; clrscr; for ACode:=1 to MaxRecords do begin if getActiveRecord(ACode) then begin getOneRecord(Acode,OneItemRecord); ShowRecordDetails(OneItemRecord); end end; closeinventoryfile; end; procedure DeleteOneRecord; //Delete a specified record //Does this by reading record, setting ItemActive to false then //writes back the record begin end; procedure EditOneRecord; //Displays specified record and allows simple editing begin end; function getChoice(x,y:integer; MaxChoice:char):char; //Returns menu choice in range 0 to MaxChoice //Prompt displayed at x,y var choice : char; begin gotoxy(x,y); write('Please enter a choice: '); choice:=readkey; while not (choice in ['0'..MaxChoice]) do begin gotoxy(x,y); write('Please enter a choice (0..',MaxChoice,'): '); choice:=readkey; //Single key input function end; getchoice:=choice; end; procedure DisplayMenu; //Menu screen var MyChoice:char; procedure MenuLine(x,y:integer; MenuLineText:string); //Displays one line of the menu begin gotoxy(x,y); write(MenuLineText); end; begin repeat clrscr; MenuLine(25,2,'Inventory Maintenance');

9.7

Programming in Pascal - 9

MenuLine(25,4,'0. Exit'); MenuLine(25,6,'1. Add new record'); MenuLine(25,8,'2. Delete inventory record'); MenuLine(25,10,'3. Edit an inventory record'); MenuLine(25,12,'4. Lookup a record'); MenuLine(25,14,'5. List active records'); MyChoice:=getChoice(25,20,'5'); clrscr; case MyChoice of '1': AddOneRecord; '2': DeleteOneRecord; '3': EditOneRecord; '4': ShowOneRecord; '5': ListActiveRecords; end; until MyChoice='0'; end; // Main program begin clrscr; DisplayMenu; writeln; writeln('Finished.'); end.

Q1 Complete the procedure DeleteOneRecord. Q2 Complete the procedure EditOneRecord Q3 Modify the programs to deal with five digit item codes. A maximum record capacity of 2000 records is required. Your program must be able to handle record clashes/synonyms.

9.8


				
DOCUMENT INFO
Shared By:
Tags: Progr, ammin
Stats:
views:268
posted:11/28/2009
language:English
pages:8
Description: Programming-in-PASCAL