T2 File Handling
T2 File Handling
Starter
• Programs make use of variables and arrays such as:
Results 🡨 [52, 87, 69, 83, 95]
CurrentResult 🡨 Results[1]
• If we are able store data
in RAM with variables
and arrays, why do we
need to store data
in files?
File handling
Unit 10 Advanced programming and databases
Starter
• Why do we need to store data in files?
• RAM is primary storage and volatile (temporary)
• Files are stored in secondary storage which is
non-volatile (permanent)
• Files may be shared with other computers or users
• If stored on shared storage, files may be accessed by
multiple users
File handling
Unit 10 Advanced programming and databases
Creating a file
• To create a new file use the code:
OPENFILE FileA.txt FOR WRITE
• A file needs to be opened
before it can be written to
• FileA.txt can now be written
to in the rest of the program
• When creating the file (in
IGCSE pseudocode) opening
the file in WRITE mode will
create a new file, losing any
original data if the file
already existed
File handling
Unit 10 Advanced programming and databases
ENDWHILE
CLOSEFILE marks.txt Close “marks.txt”
File handling
Unit 10 Advanced programming and databases
Worksheet 2
• Now complete Task 1 on Worksheet 2
File handling
Unit 10 Advanced programming and databases
Closing a file
• Once you have finished reading or writing to a file it
is important to close it
• This frees up any memory used by having it open
• Once closed, the file cannot be read from or written to without
opening it again
• To close a file, use:
CLOSEFILE filename
File handling
Unit 10 Advanced programming and databases
Worksheet 2
• Now complete Task 2 on Worksheet 2
File handling
Unit 10 Advanced programming and databases
Plenary
• With a partner, answer the
following questions:
• What are the three steps required
to read from, or write to, a file?
• What are the two modes that
files may be opened in?
File handling
Unit 10 Advanced programming and databases
Plenary
• What are the three steps required to read from, or
write to, a file?
• Open, Read/Write, Close
Copyright
This unit and all the worksheets, PowerPoint presentations, teaching guides and other associated files
distributed with it are supplied to you by PG Online Limited under licence and may be used and copied by you
only in accordance with the terms of the licence. Except as expressly permitted by the licence, no part of the
materials distributed with this unit may be used, reproduced, stored in a retrieval system, or transmitted, in any
form or by any means, electronic or otherwise, without the prior written permission of PG Online Limited.
Licence agreement
This is a legal agreement between you, the end user, and PG Online Limited. This unit and all the worksheets,
PowerPoint presentations, teaching guides and other associated files distributed with it is licensed, not sold, to
you by PG Online Limited for use under the terms of the licence.
The materials distributed with this unit may be freely copied and used by members of a single institution on a
single site only. You are not permitted to share in any way any of the materials or part of the materials with any
third party, including users on another site or individuals who are members of a separate institution. You
acknowledge that the materials must remain with you, the licencing institution, and no part of the materials may
be transferred to another institution. You also agree not to procure, authorise, encourage, facilitate or enable any
third party to reproduce these materials in whole or in part without the prior permission of PG Online Limited.