0% found this document useful (0 votes)
19 views4 pages

11.4 Text File Handling EMK Notes 2024

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views4 pages

11.4 Text File Handling EMK Notes 2024

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

11.

4 Text File Handling

Candidates should be able to:


 Show understanding of why files are needed paperscambridge.com
 Write pseudocode to handle text files that consist of one or more lines.

Data need to be stored permanently. One approach is to use a file. Computer


programs store data that will be required again in a file.
Every file is identified by its filename. Text file consists of a sequence of characters
formatted into lines. Each line is terminated by an end-of-line marker. Text file is
terminated by an end-of-file marker.
Text File Operation

To Open a file before reading from it or writing to it:


OPENFILE < file identifier > FOR <file mode>
Files can be opened in one of following modes:
 READ Mode: Reads data from the file.
 WRITE Mode: Writes data to file, any existing data stored in file will be overwritten.
 APPEND Mode: Adds data to end of the file. Any existing data stored in file will be
not be overwritten.
➢ Once file is opened in READ mode, it can be read from a line at a time:
READFILE < file identifier >, < variable>

➢ Once file is opened in WRITE or APPEND mode, it can be written to a line at a time:
WRITEFILE < file identifier >, <variable>
Note: In both cases, variable must be of data type STRING.

➢ EOF Function is used to test for end of a file. It returns a value TRUE if end of a file
has been reached and FALSE otherwise.
EOF (<File Identifier>)
➢ When a file is no longer being used it should be closed:
CLOSEFILE <file identifier>
 Writing Text Into File:
 pseudocode statements provide facilities for writing to a file:
Following
OPENFILE <filename> FOR WRITE // open the file for writing
WRITEFILE <filename >, <stringValue> // write a line of text to the file
CLOSEFILE <filename > // close file

 Reading from a text file: An existing file can be read by a program.


OPENFILE <filename> FOR READ // open file for reading
READFILE <filename>, <stringVariable> // read a line of text from the file
CLOSEFILE <filename> // close file

Computer Science IGCSE, O & A level By Engr M Kashif 03345606716


11.4 Text File Handling

 Appending to a text file:


Sometimes we may wish to add data to an existingpaperscambridge.com
file rather than create a new
file. This can be done in Append mode. It adds new data to end of existing file.
OPENFILE <filename> FOR APPEND // open file for append
WRITEFILE <filename>, <stringValue> // write a line of text to the file
CLOSEFILE <filename> // close file

The end-of-file (EOF) marker:


To read a file from beginning to end we can use a conditional loop. Text files contain a
special marker at end of file that we can test for. Every time EOF() function is called it
will test for this marker. Function will return FALSE if end of file is not yet reached and
will return TRUE if end -of-file marker has been reached.
We use construct REPEAT... UNTIL EOF(). If it is possible that file contains no data, it is
better to use construct WHILE NOT EOF()
Example: Following pseudocode statements read a text file and output its contents:
OPENFILE "Test. txt" FOR READ
Declare TextString : String
WHILE NOT EOF( "Test.txt" )
READFILE "Test. txt", TextString
OUTPUT TextString
ENDWHILE
CLOSEFILE "Test. txt"
This pseudocode shows how file myText.txt could be written to and read from:
DECLARE textLine : String
DECLARE myFile : String
myFile  “myText.txt”
OPENFILE myFile FOR WRITE
REPEAT
OUTPUT “Enter Line of Text”
INPUT textLine
IF textLine <> “ ”
THEN
WRITEFILE myFile, textLine
ELSE
CLOSEFILE(myFile)
ENDIF
UNTIL textLine = “ ”

OUTPUT “ The file contains these lines of text ”


OPENFILE myFile FOR READ
REPEAT
READFILE myFile, textLine

Computer Science IGCSE, O & A level By Engr M Kashif 03345606716


11.4 Text File Handling

OUTPUT textLine
UNTILL EOF( myFile) paperscambridge.com
CLOSEFILE (myFile)
Exam Style Question
ESQ 1: Algorithm will process data from a test taken by a group of students. Algorithm
will prompt and input name and test mark for 35 students. Algorithm will add names
of all students with test mark of less than 20 to existing text file Support_List.txt which
already contains data from other group tests. 9618 P22 Oct 22
(i) Describe steps that algorithm should perform. Do not include pseudocode in your
answer. [5]
Ans: 1. Open file in APPEND mode
2. Prompt and Input a student name and mark
3. If mark greater than or equal to 20 jump to step 5
4. Write only the name to the file
5. Repeat from Step 2 for 35 times.
(ii) Explain why it is better to store names of students in a file rather than in array.
Ans: Data in a file is saved after computer is switched off and stored permanently. No
need to re-enter data when program is re-run.
(iii) Explain why WRITE mode cannot be used in the answer to part (a)(i)
Ans: So that existing file data is not overwritten.
ESQ # 2: LogArray is 1D array containing 500 elements of type STRING. A procedure,
LogEvents, is required to add data from array to end of existing file LoginFile.txt
Unused array elements are assigned value "Empty". These can occur anywhere in array
and should not added to file. Write pseudocode for procedure LogEvents. 9618-SP-21
Solution:
PROCEDURE LogEvents ( )
DECLARE FileData : STRING
DECLARE ArrayIndex : INTEGER
OPENFILE "LoginFile.txt" FOR APPEND
FOR ArrayIndex ← 1 TO 500 // 0 TO 499
IF LogArray[ArrayIndex] < > "Empty"
THEN
FileData ← LogArray[ArrayIndex]
WRITEFILE "LoginFile.txt", FileData
ENDIF
NEXT
CLOSEFILE "LoginFile.txt"
ENDPROCEDURE
ESQ# 3 : A procedure Preview( ) will: 9618 P21 May 21
take name of a text file as a parameter
output a warning message if file is empty
otherwise output first five lines from file (or as many lines as there are in file
if this number is less than five).
Write pseudocode for the procedure Preview()

Computer Science IGCSE, O & A level By Engr M Kashif 03345606716


11.4 Text File Handling

Ans:
paperscambridge.com

ESQ:4 A procedure LastLines() will: 9618 P22 May 22


• take name of a text file as a parameter
• output last three lines from that file, in same order as they appear in file.
• Use local variables LineX, LineY and LineZ to store the three lines from file.
• You may assume file exists and contains at least three lines.
Write pseudocode for procedure LastLines().

**************

Computer Science IGCSE, O & A level By Engr M Kashif 03345606716

You might also like