Data in variables disappears when a program ends; data in a file does not. Learn how to open files in READ, WRITE, APPEND and RANDOM mode, read and write records, add a record to a serial file and insert one into a sequential file, and use SEEK, GETRECORD and PUTRECORD to go straight to a record in a random file.
A file must be opened in a mode before use and closed afterwards. READ reads lines from the start; WRITE creates a new file (any existing data is lost); APPEND adds to the end; RANDOM opens a file of fixed-length records for direct access. In a serial file records are stored in the order they were added; in a sequential file they are stored in order of a key field, so inserting a record means copying to a new file. In a random file each record is at an address, often calculated from its key by a hashing function, and SEEK moves the file pointer straight there so GETRECORD and PUTRECORD can read or replace that record.
A text file is a series of lines read and written one after another as strings. A record can be stored as one line, with the fields in fixed positions. Run the file programs in the lab to see lines being written and read back.
| Statement | Effect |
|---|---|
| OPENFILE "Members.txt" FOR READ | opens the file so lines can be read from the start |
| OPENFILE "Members.txt" FOR WRITE | creates a new file; any data already in a file of that name is lost |
| OPENFILE "Members.txt" FOR APPEND | opens the file so new lines are added after the existing data |
| READFILE "Members.txt", LineOfText | reads the next line into a STRING variable |
| WRITEFILE "Members.txt", LineOfText | writes the data as a new line (WRITE or APPEND mode) |
| EOF("Members.txt") | TRUE when there are no more lines to read |
| CLOSEFILE "Members.txt" | closes the file when it is no longer needed |
Each line of Members.txt holds a 4-character member ID followed by a name, for example M007Lee. This function searches the file for an ID and returns the name, or an empty string if it is not there:
FUNCTION FindMember(SearchID : STRING) RETURNS STRING
DECLARE LineOfText : STRING
DECLARE Found : STRING
Found ← ""
OPENFILE "Members.txt" FOR READ
WHILE NOT EOF("Members.txt") AND Found = ""
READFILE "Members.txt", LineOfText
IF MID(LineOfText, 1, 4) = SearchID THEN
Found ← MID(LineOfText, 5, LENGTH(LineOfText) - 4)
ENDIF
ENDWHILE
CLOSEFILE "Members.txt"
RETURN Found
ENDFUNCTION
OPENFILE "Members.txt" FOR APPEND / WRITEFILE "Members.txt", "M060" & "Diaz" / CLOSEFILE "Members.txt". APPEND, not WRITE: opening in WRITE mode would create a new, empty file and lose every existing member.Use WHILE NOT EOF(...) before each READFILE, not a FOR loop with a guessed number of lines. EOF is also TRUE straight away for an empty file, so the loop correctly runs zero times.
Both kinds are read from the beginning, one record after another. The difference is the order the records are stored in, and that changes how records are added and found.
| Serial file | Sequential file | |
|---|---|---|
| Order of records | the order they were added (chronological) | sorted by a key field, e.g. member ID |
| Adding a record | append it to the end | copy records to a new file, writing the new record in its correct place |
| Searching | read every record until found or the end of the file | can stop as soon as a key greater than the one wanted is read |
| Typical use | a log of transactions as they happen | a master file processed in key order in a batch (e.g. payroll) |
To insert NewLine into the sequential file Members.txt, every record is copied to NewMembers.txt and the new record is written just before the first record with a larger key:
DECLARE LineOfText : STRING
DECLARE NewLine : STRING
DECLARE Inserted : BOOLEAN
NewLine ← "M020Patel"
Inserted ← FALSE
OPENFILE "Members.txt" FOR READ
OPENFILE "NewMembers.txt" FOR WRITE
WHILE NOT EOF("Members.txt")
READFILE "Members.txt", LineOfText
IF Inserted = FALSE AND MID(LineOfText, 1, 4) > MID(NewLine, 1, 4) THEN
WRITEFILE "NewMembers.txt", NewLine
Inserted ← TRUE
ENDIF
WRITEFILE "NewMembers.txt", LineOfText
ENDWHILE
IF Inserted = FALSE THEN
WRITEFILE "NewMembers.txt", NewLine
ENDIF
CLOSEFILE "Members.txt"
CLOSEFILE "NewMembers.txt"
You cannot insert a line into the middle of a text file in place. For a sequential file the answer must copy the records to a new file (and then, usually, the new file replaces the old one).
Four quick checks. Each correct answer earns XP and records this skill on your profile.
Which file mode creates a new file, losing any data already in a file of that name?
In a sequential file, how are the records stored?
In the lab, what must happen before a program can read lines from a file it has just written?
Which command reads the record at the file pointer of a random file?
A random file stores fixed-length records, usually of a user-defined record type. Because every record is the same length, the record at any address can be reached directly without reading the ones before it.
TYPE StockItem
DECLARE ItemCode : INTEGER
DECLARE Description : STRING
DECLARE Quantity : INTEGER
ENDTYPE
DECLARE Item : StockItem
DECLARE Address : INTEGER
Address ← 1047 MOD 500 // hashing function: address 47
OPENFILE "Stock.dat" FOR RANDOM
SEEK "Stock.dat", Address
GETRECORD "Stock.dat", Item
Item.Quantity ← Item.Quantity - 3
SEEK "Stock.dat", Address
PUTRECORD "Stock.dat", Item
CLOSEFILE "Stock.dat"
OPENFILE "Queue.dat" FOR RANDOM, then FOR Position ← 6 TO 1 STEP -1.SEEK "Queue.dat", Position, GETRECORD "Queue.dat", Entry, SEEK "Queue.dat", Position + 1, PUTRECORD "Queue.dat", Entry, then NEXT Position.SEEK "Queue.dat", 1, PUTRECORD "Queue.dat", NewEntry, CLOSEFILE "Queue.dat".Always SEEK before PUTRECORD when updating a record you have just read. Re-seeking the same address makes certain the changed record replaces the original rather than being written somewhere else.
Unlocks once the four checks above are done. Worth more XP, written to A Level Paper 3 and Paper 4 standard.
Records are stored at addresses given by Key MOD 100. Which key collides with key 4271?
Why must a new record be inserted into a sequential text file by copying to a new file?
A file of daily sales transactions is added to as each sale happens. Which organisation is most suitable?
What does EOF("Data.txt") return when Data.txt is an empty file opened for READ?
This skill is now recorded on your profile. Keep the chain going.