Files and Exception Handling (Cs A Level)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

AS & A LEVEL COMPUTER SCIENCE 9618

FILE PROCESSING
Note: General knowledge on Files in paper 2 is a must
TYPES OF FILES
Binary Files
1. .dat is the extension
2. Data is stored in binary format directly
3. Can be accessed directly
Text Files
1. .txt is the extension
2. These are accessed sequentially

1 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618

RANDOM FILES

RANDOM FILES

Read or write at the same line


Data can be accessed directly
1. Random files contain a collection of data normally as records of fixed length
2. They can be thought of as having a file pointer which can be moved to any
location or address in a file
3. The record at that location can then be read or written

2 Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618

RANDOM FILES

OPENFILE <Filename> FOR RANDOM (This opens a random file)

Note: The SEEK command moves the file pointer to a given location
SEEK <filename>, <address> Goes to the given address

Note: The command GETRECORD is used to read the record at the file pointer
GETRECORD <filename>, <identifier> get a particular record
Note2: When GETRECORD command is executed, the variable is assigned to the
record that is read

Note: The command PUTRECORD is used to write a record into the file at the file
pointer
PUTRECORD <Filename>, <Identifier> Put a particular record into the
file

3
Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618

RANDOM FILES
PSEUDOCODE EXAMPLE

TYPE Book
DECLARE BOOKID : INTEGER
DECLARE BOOK_TITLE : STRING
DECLARE AUTHOR : STRING
ENDTYPE

DECALRE novel : Book

novel.BOOKID = 232
novel.BOOK_TITLE = “Understanding Java Programming”
novel.AUTHOR = “Kazibwe Davis”

4
Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618

RANDOM FILES
PSEUDOCODE EXAMPLE

OPENFILE “KIS.dat” FOR RANDOM

Address = Hash ( novel.BOOKID)


SEEK “KIS.dat”, Address
PUTRECORD “KIS.dat”, Novel

CLOSEFILE “KIS.dat”

Qtn: Imagine there is a file with 10 records “Random.dat”. Ask from user book ID
and print the book name

5
Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618

RANDOM FILES
PSEUDOCODE EXAMPLE

OPENFILE “Random.dat” FOR RANDOM

OUTPUT “ Enter Book ID”


INPUT BookID
Address = Hash( BookID )
SEEK “Random.dat”, Address
GETRECORD “Random.dat”, Data
OUTPUT Data.BookName

CLOSEFILE “Random.dat”

6
Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618

EXCEPTION HANDLING
EXCEPTION
It is unplanned event and situation causing a crash with in a computer program
EXCEPTION HANDLING
This refers to block of code which is called when a runtime error occurs to avoid
the program from crashing
SITUATIONS WHERE EXCEPTION HANDLING IS REQUIRED
Division by zero
Run-time error
File does not exist
Invalid array index
Hardware failure

7
Davis_Kazibwe@2023KIS
AS & A LEVEL COMPUTER SCIENCE 9618

EXCEPTION HANDLING
BENEFITS OF USING EXCEPTION HANDLING IN A PROGRAM
1. The program will not crash
2. Results does not cause further error
3. Appropriate error message
4. Exceptional conditions are identified
5. Improve readability

8
Davis_Kazibwe@2023KIS

You might also like