Professional Documents
Culture Documents
10.3 As File Handling
10.3 As File Handling
10.3 Files
show understanding of why files are needed
Write pseudocode to handle text files that consist of one or more lines
https://www.bbc.co.uk/bitesize/guides/zb3yb82/revision/6
Text files consist of lines of text that are read or written consecutively as strings.
Why files are needed?
Files are used to store data permanently.
When a program is being executed data is stored in variables, constants, arrays, lists etc. These data
structures use RAM, making them temporary. While files stores data in secondary storage devices
like HDD or SSD. These are permanent and non-volatile. They retain data even after computer is
switched OFF, so the data can be accessed next time program is run. Data can be used by more
than one program. Data can be backed-up. It can be transferred be transferred from one place to
another, from one system to another.
A file must be opened in a specified mode before any file operations are attempted. This is written as
follows:
OPENFILE <File identifier> FOR <File mode>
The file identifier may be a literal string containing the file names, or a variable of type STRING that
has been assigned the file name.
File Modes: Following are file mode:
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.
Writing to file
Pseudo code Python
OPENFILE <filename> FOR WRITE FileHandle = open (<FileName> , "w" )
WRITEFILE <filename> <stringvalue> FileHandle.write(<StringValue>)
CLOSEFILE <filename> FileHandle.close()
OPENFILE "test.txt" FOR WRITE FileHandle = open ("test.txt" , "w" )
WRITEFILE “test.txt”, TextLine FileHandle.write(TextLine)
CLOSEFILE “test.txt” FileHandle.close()
01/07/2015 FALSE
Each date and discount indicator is separated by a single <Space> character.
The discount indicators are:
• FALSE – indicates a date on which no discount is offered
• TRUE – indicates a date on which a discount is offered
.......................................................................................................................................[1]
(iv) this
issue.
.......................................................................................................................................[1]
The procedure is tested using the file MyFile.txt that contains 100 lines of text.
The procedure gives the expected result when called as follows:
CALL OutputLines("MyFile.txt", 1, 10)
(a) The procedure is correctly called with three parameters of the appropriate data types, but the
procedure does not give the expected result.
Give three different reasons why this might happen.
1 ................................................................................................................................................
...................................................................................................................................................
2 ................................................................................................................................................
...................................................................................................................................................
3 ................................................................................................................................................
................................................................................................................................................... [3]
(b) Write pseudo code for the procedure OutputLines().
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................
...................................................................................................................................................