Subtopic Notes

20.2 - File Processing and Exception Handling

20. Further Programming

Handling text files

  • Text files consist of lines of text that are read or written consecutively as strings
  • The purpose of storing data in a file is to be used again by a program
  • Data in files are stored permanently, meaning they can be used later
  • It is good practice to explicitly open a file, stating the mode of operation, before reading from or writing to it.
  • File can be opened in three modes:
    • READ: for data to be read from the file
    • WRITE: for data to be written to the file. A new file will be created and any existing data in the file will be lost
    • APPEND: for data to be added to the file, after any existing data
  • A file should be opened in only one mode at a time

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

Opening a file

OPENFILE <File identifier> FOR <File mode>

The file identifier will be the name of the file with a data type of string.

Reading from a file

Data is read from the file, after opening in READ mode, using the following:

READFILE <file Identifier>, <variable>

When the command is executed, the data item is read and assigned to the variable. The data type is string.

Checking end of file

To test whether there are any more lines to be read from the file:

EOF(<file identifier>)

This function returns TRUE if there are no more lines to read, or if an empty file has been opened in READ mode, and FALSE otherwise.

Writing to a file

Data is written into the file after opening using the following command:

WRITEFILE <file identifier>, <variable>

When the command is executed, the data is written into the file.

Closing a file

Files should be closed when they are no longer needed.

CLOSEFILE <file identifier> //Closes the file

Example: copying all lines from one file to another

This example uses the operations together, to copy all the lines from FileA.txt to FileB.txt, replacing any blank lines by a line of dashes.

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Handling random files

  • Random files contain a collection of data, normally as records of fixed length.
  • They can be thought of as having a file pointer which can be moved to any location or address in the file.
  • The record at that location can then be read or written.

Random files

Random files are opened using the RANDOM file mode as follows:

OPENFILE <file identifier> FOR RANDOM

As with text files, the file identifier will normally be the name of the file.

Moving the file pointer

The SEEK command moves the file pointer to a given location:

SEEK <file identifier>, <address>

The address should be an expression that evaluates to an integer which indicates the location of a record to be read or written. This is usually the number of records from the beginning of the file. It is good practice to explain how the addresses are computed.

Reading a record

The command GETRECORD should be used to read the record at the file pointer:

GETRECORD <file identifier>, <variable>

When this command is executed, the record that is read is assigned to the variable which must be of the appropriate data type for that record, usually a user-defined type.

Writing a record

The command PUTRECORD is used to write a record into the file at the file pointer:

PUTRECORD <file identifier>, <variable>

When this command is executed, the data in the variable is inserted into the record at the file pointer. Any data that was previously at this location will be replaced.

Example: handling random files

The records from positions 10 to 20 of a file StudentFile.Dat are moved to the next position and a new record is inserted into position 10.

The example uses the user-defined type Student defined in previous section.

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Serial access (Writing Records One After Another)

Serial access processes data in the order it is received.

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

Sequential access (Reading All Records in Order)

Sequential access reads records from start to end.

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

Random Access (Accessing a Specific Record)

Random access allows direct access to a specific record.

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

Exception Handling

  • Refers to the way a program detects and responds to such errors, allowing it to recover or terminate gracefully instead of crashing.
  • Exception: Unexpected event that interrupts the normal flow of a program
  • Uses
    • Prevents programs from crashing suddenly
    • Provides clear and user-friendly error messages
    • Makes software more stable and reliable
    • Manages common errors
  • Causes of exceptions
    • Programming errors - such as using uninitialized variables or making logical mistakes.
    • Hardware or device failures - like a disk read error, printer disconnection, or other hardware malfunctions.
    • Division by zero
    • File errors - when a file is missing, unreadable, or cannot be accessed.
    • Invalid input

Exception Handling Using try and except (Python)

Example: File Not Found

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

Multiple Exception

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

File Handling with Exception Handling

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.