Tutorial 11
💾
Files
9618 A-Level
In this tutorial, we'll look at the 3 file modes require for AS (READ, WRITE & APPEND and 1 additional file mode required for A2: RANDOM
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 AS, there are three file modes - READ, WRITE and APPEND:
- READ: opens a file that already exists to be read
- WRITE: creates a new file/overwrites an existing file to write to
- APPEND: creates a file if it doesn't exist, else adds lines to the end of an existing file
The following example shows us opening and closing a file in these modes - we aren't actually reading, appending 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:
If in A2, the following file mode is also required:
- RANDOM: creates a new file if it doesn't exist to allow records to be added to or removed from
Again, the below example simple shows us opening and closing a record - not actually putting or getting a record from the file
2
Writing to a File
We can use WRITEFILE followed by the file and data to write
As stated, opening a file in WRITE mode will create a new file of the given name, regardless of it the file already exists - effectively, an existing file will be overwritten with the new content
3
Appending to a File
If we open the file in APPEND mode, new lines will be added to the end of an existing file (or a file will be created if it doesn't exist)
As with WRITE mode, we use the same WRITEFILE keyword followed by the file and data to write
4
Reading from a File
In AS, if we want to read the entire file, we can make use of the EOF function that will return whether we have reached the "end of file" - i.e. read all the lines
As an example, we can read all lines in the palindrome file:
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"
Note: if you know the number of lines in the file (in this case 3), you could loop from 1 to 3 and use READFILE inside the loop, but generally using a while loop is recommended, since this also works when we don't know the number of lines in the file
You would rarely do this, but this just demonstrates how EOF works - once all 3 lines have been read, EOF will now return true, symbolising there's no more lines to read
5
Random Files
When deadling with random files, the following keywords are required
- SEEK file, position: go to a specific record position in the file
- PUTRECORD file, record: place the record in the file at the position we have seeked to
- GETRECORD file, variable: read the record from the file at the position we have seeked to into the variable specified
As you can see, if using either PUTRECORD or GETRECORD, we first have to SEEK to the desired record position in the file
Assume we have 5 records - we would then seek to positions 1 to 5 in the file
The following demonstrates the all functionality associated with random files - note the following:
- Defining a record (Team), creating an array of these records and initialising them with values
- Seeking to file positions 1 & 2, then putting the relevant records in those positions - usually you would do this in a loop, here I just did it outside of a loop to make it obvious what the seek positions should be
- Looping from 1 to 2 and reading the records from the file at this index into a new team array (a new array was created just to demonstrate that GETRECORD works)
6
Technology Quotes
Create a program to output a numbered list of all the technology quotes in the following numbered format, with each quote having an empty line between them
7
Student Scores
Use the student names and scores to create a program that outputs the data in the following format: "Albert scored 62"
The min/max score and student name who, scored that the average and the number who passed/failed (with e.g. 50% being the passing score) should also be output
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
8
Character Ranges
Create a program that allows the user to specify two characters, then will write all characters including and between the two characters specified to an existing file
This should continue, until the two characters the user enters are the same - this character shouldn't be written to the file
9
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
10
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
11
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
12
Text Editor
Create a text editor/notes app that asks you to enter a filename and whether you want to read, write or append from/to it. An exit option should be provided too
If they choose to read, all lines should be displayed
If they choose to write/append, they should be prompted with the line they want to write
13
Capital City Quiz
Create a program that reads the csv file into a 2D array to store the 248 countries and capital cities
The program should choose 10 random countries and ask the user to name the capital city - an appropriate message should be displayed depending on if they were right or wrong
Their overall score should be output at the end
14
Periodic Table (RANDOM)
Note: this question is for A2 - if you're in AS, you can skip it
Create a program that stores basic information (name, symbol, atomicNumber, mass, isMetal etc) about several chemical elements - these should be written to a file
Then create another program that allows the user to case-insensitively search chemical elements by name and displays the chemical's details - an empty input should terminate the program
Program to read file and let user search for elements:
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