Algorithm - Relative and sequential files
Processing sequential files
Creating a file
Sequential files need to be opened for output when they are created and must be closed before the program ends.
A record containing sentinel values should be written as the last record in the file before it is closed, so that other programs using that same file do not have to know how many records there are in that file.
Note: It is also possible to use the system flag EOF which is used to detect the end of a sequential file in many languages.
Every language has its own particular syntax, but in an algorithm it is appropriate to include generic statements as shown in the following algorithm.
The following example writes 10 records to a sequential file called FriendsData. The data is entered by the user from the keyboard:
BEGIN CreateAFile
Open FriendsData for output
FOR i = 1 to 10
Display “Please enter the details for the next person: ”
Get fname, sname, emailaddr, mobile
Write FriendsData from fname, sname, emailaddr, mobile
NEXT i
Let fname = “xxx”
Let sname = “xxx”
Write FriendsData from fname, sname, emailaddr, mobile
Close FriendsData
END CreateAFile
Printing the contents of a file
Sequential files that already exist need to be opened for input before they can be read, and closed before the program ends.
It is best to use a priming read as in the following algorithm, in case the located file contains no records in it. It also ensures that the sentinel values do not get processed or printed.
BEGIN DisplayFileContents
Open FriendsData for input
Read fname, sname, emailaddr, mobile from FriendsData
’This is a priming read, performed just before entering the loop to provide the first record (if there is one) for printing
WHILE fname <> “xxx” AND sname <> “xxx”
Display fname, sname, emailaddr, mobile
Read fname, sname, emailaddr, mobile from FriendsData
’this reads subsequent records which can then be tested for the sentinel value before they are processed
END WHILE
Close FriendsData
END DisplayFileContents
Appending records to an existing sequential file
To enable new records to be added at the end of an existing sequential file, it must be opened so that records can be appended and closed before the program ends. To open it for this purpose, use the format Open filename for append.
This algorithm asks for input from the keyboard for a series of new records to be added to an existing file.
Note: Where records are to be appended to an existing file, that file should not already include a record with sentinel values as its last record. If it does, that record will remain in the file immediately preceding the newly appended records.
BEGIN AddNewRecords
Open FriendsData for append
Display “Please enter the details for the first new person to be added: ”
Display “Enter xxx for both first name and surname to indicate there are no more records to be added.”
Get fname, sname, emailaddr, mobile
WHILE fname <> “xxx” AND sname <> “xxx”
Write FriendsData from fname, sname, emailaddr, mobile
Display “Please enter the details for the next new person to be added:”
Display “Enter xxx for both first name and surname to indicate there are no more records to be added”
Get fname, sname, emailaddr, mobile
END WHILE
Close FriendsData
END AddNewRecords
Processing relative files
Creating a relative file
Relative files need to be opened for relative access when they are created and must be closed before the program ends. All records are accessed through the use of a key which specifies the relative position of that record within the file. The key field used must contain positive integer values only. There is no sentinel value written as the file is not accessed sequentially.
The following example writes 10 records to a relative file called ProductData. The data is entered by the user from the keyboard and the products are entered in no particular order:
BEGIN CreateARelativeFile
Open ProductData for relative access
FOR i = 1 to 10
Display “Please enter the details for the next product: ”
Get ProdNumber, description, quantity, price
Write ProductData from ProdNumber, description, quantity, price using ProdNumber
’note the use of the variable ProdNumber as the key field, specifying where this record will be written in the file.
NEXT i
Close ProductData
END CreateARelativeFile
Reading records from a relative file
Relative files need to be opened for relative access when they are accessed for either input or output, and must be closed before the program ends. All records are accessed through the use of a key which specifies the relative position of that record within the file. The key field used must contain positive integer values only.
BEGIN ReadRecordsFromARelativeFile
Open ProductData for relative access
REPEAT
Display “Please enter the product number for the next product you wish to see:”
Display “Please enter 999 when you are done”
Get RequiredProdNumber
Read ProductData into ProdNumber, description, quantity, price using RequiredProdNumber
’note the use of the variable RequiredProdNumber as the key field, specifying where this record will be found in the file
IF RecordNotFound THEN
’note the use of the flag RecordNotFound returned by the operating system
Display “Sorry – no such product”
ELSE
Display ProdNumber, description, quantity, price
END IF
UNTIL RequiredProdNumber = 999
Close ProductData
END ReadRecordsFromARelativeFile
Updating records in a relative file
Relative files need to be opened for relative access when they are updated and must be closed before the program ends. All records are accessed through the use of a key which specifies the relative position of that record within the file. The key field used must contain positive integer values only.
The following algorithm allows the price of any product to be changed.
BEGIN UpdateRecordsInARelativeFile
Open ProductData for relative access
Display “Please enter the product number for the next product whose price you wish to update:”
Display “Please enter 999 when you are done”
Get RequiredProdNumber
’priming read in case they wish to exit immediately by entering 999
WHILE RequiredProdNumber < > 999
NotFound = 0
Read ProductData into ProdNumber, description, quantity, price using RequiredProdNumber
’note the use of the variable RequiredProdNumber as the key field, specifying where this record will be found in the file
IF RecordNotFound THEN
’note the use of the flag RecordNotFound returned by the operating system
Display “Sorry – no such product”
NotFound = 1
ELSE
Display ProdNumber, description, quantity, price
Display “ Is this the correct product?”
Get Reply
END IF
’do not update until the correct product record is retrieved
IF NotFound = 0 AND Reply = “Y” THEN
Get NewPrice
Write ProductData from ProdNumber, description, quantity, NewPrice using ProdNumber
’update record using data for the new price and the existing data in the other fields
END IF
Display “Please enter the product number for the next product whose price you wish to update:”
Display “Please enter 999 when you are done”
Get RequiredProdNumber
ENDWHILE
Close ProductData
END UpdateRecordsInARelativeFile
MINI TASK
Follow this Visual Studio C# tutorial on how to open, edit and save a txt file using arrays. This is particularly important to understand when tackling your first assignment.