Tutorial 11
💾
Files
2210 O-Level
In this tutorial, we'll look at how to both read from and write to files
It is important to remember that when we open a file using OPENFILE, we must close it with a corresponding CLOSEFILE later in the program - this is so the file lock can be removed and it can be modified by another process (you don't want two processes writing to the same file at the same time, since that could cause data corruption/inconsistency)
Note: there are some example files at the bottom of the page you can use to test your programs
1
File Modes
For IG, there are only two file modes - READ and WRITE
- READ: opens a file that already exists to be read
- WRITE: creates a new file/overwrites an existing file to write to
The following example shows us opening and closing a file in these modes - we aren't actually reading or writing any data here
Note: as shown below, file names can be either in or out of quotes
We can also use a constant or variable to store filenames:
2
Writing to a File
We can use WRITEFILE followed by the file and data to write
3
Reading from a File
For IGCSE/O-Level, there is no way to determine the number of lines in a file/when we reach the end of the file - hence if you ever have to read a file, we should be told the number of lines the file contains
As an example, reading from the palindrome file we just created, we can simply read 3 lines from the file by doing this:
READFILE is followed by the file we are reading from, then a variable where we will store the line we just read - here, I simply called it "line"
If this is a bit confusing, here is equivalent code outside a loop - of course a loop is more flexible/convenient especially for files with huge or unknown numbers of lines (AS)
4
Technology Quotes
Create a program to output a numbered list of all the 59 technology quotes in the following numbered format, with each quote having an empty line between them
5
Student Scores
Use the 12 student names and 12 scores to create a program that outputs the data in the following format: "Albert scored 62"
Note: the student names/scores are in the same order in both files - i.e. line 1 refers to student 1's score, line 2 to student 2's score etc
Extension: you could use the AS STR_TO_NUM function to convert the scores into integers - which you could then find the min/max score/student, the average, the number who passed/failed (with e.g. 50% being the passing score)
6
Movie Quotes from Decade
Use the 20 movie quotes to create a program that allows people to input a decade, then outputs all the movie quotes from that decade along with the count
If no movie quotes exist for that decade, a message notifying the user of this should be displayed
The program should continue until the user enters nothing for the decade
7
Remove Blank Lines
Note how the technology facts is poorly formatted with many blank lines - create a program that will copy all of the facts and exclude the empty lines to a new text file
A count of the number of lines written and number of empty lines removed should also be displayed
8
Random Joke
Create a program that reads all 12 jokes into a 2D array, with the 1st column containing the question and the 2nd column containing the answer/joke (note: if the joke consists of just a single line, then the 2nd line in the file has been left blank)
The user should then be continually asked how many jokes they want to hear - entering 0 or less should result in the program terminating
The following are some example files you can use throughout these challenges - you may also want to use a list of English words to try some interesting tasks too