0% found this document useful (0 votes)
48 views

T2 File Handling

Programming Concepts Unit 8 - File handling

Uploaded by

Nazril Rosly
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

T2 File Handling

Programming Concepts Unit 8 - File handling

Uploaded by

Nazril Rosly
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Objectives

• Understand the purpose of storing data in a file to be


used by a program
• Open, close and use a file for reading and
writing including:
• Read and write single items of data
• Read and write a line of text
File handling
Unit 10 Advanced programming and databases

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

Reading and writing to files


• In a programming language,
when we read from, or write
to a file what three
steps always need
to be taken?
File handling
Unit 10 Advanced programming and databases

Reading and writing to files


• Reading from and writing to
files requires the
following steps:
• Open the file
• Read from, or write to, the file
• Close the file

• If the file doesn’t yet exist it


will need to be created first
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

Reading from a text file


• Reading from a text file involves
opening the file, processing each line
then closing the file
OPEN marks.txt FOR READ Open “marks.txt”

WHILE NOT EndOfFile DO Loop through each line in the file

READFILE marks.txt, line Read the next line

OUTPUT line Output the line

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

Writing data to a text file


• Here is an algorithm which writes to a text file:
OPENFILE marks.txt FOR WRITE
INPUT name
INPUT mark
record 🡨 name + "," + mark + "\n"
WRITEFILE marks.txt, record
CLOSEFILE marks.txt
• If the user’s name is “Sandra” and their mark is 85
the following will be added to the file:
Sandra, 85
• \n means create a new line in the file
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

• What are the two modes that files may be opened


in?
• READ, WRITE
File handling
Unit 10 Advanced programming and databases

Copyright

© 2021 PG Online Limited

The contents of this unit are protected by 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.

You might also like