โก Quick Summary: File Handling Keywords
- OPENFILE "name" FOR READ: Opens a file so you can read data from it
- OPENFILE "name" FOR WRITE: Opens/creates a file to write data (overwrites existing data)
- OPENFILE "name" FOR APPEND: Opens a file to add new data to the end without erasing existing data
- READFILE "name", Variable: Reads one line from the file into a variable
- WRITEFILE "name", Data: Writes data to the file
- CLOSEFILE "name": Closes the file โ always do this at the end!
- EOF("name"): Returns TRUE if the end of the file has been reached
In This Guide:
File Opening Modes
Before you can read from or write to a file in pseudocode, you must open it using OPENFILE with the correct mode. There are three modes:
Opens an existing file to read data from it. Does not change the file contents.
Creates a new file or overwrites an existing file. All previous data is lost.
Opens an existing file and adds new data to the end, keeping the existing data.
Reading From a File
To read data from a file, you open it in READ mode, then use READFILE to get one line at a time, and loop until the end of file (EOF) is reached.
DECLARE StudentName : STRING // Step 1: Open the file for reading OPENFILE "students.txt" FOR READ // Step 2: Read all lines until end of file WHILE NOT EOF("students.txt") READFILE "students.txt", StudentName OUTPUT StudentName ENDWHILE // Step 3: Always close the file CLOSEFILE "students.txt"
Writing to a File
To write data to a file, open it in WRITE mode. Be careful โ this will overwrite all existing data in the file.
DECLARE Name : STRING DECLARE i : INTEGER // Opens the file โ creates it if it doesn't exist OPENFILE "output.txt" FOR WRITE // Write 3 names to the file FOR i โ 1 TO 3 OUTPUT "Enter a name:" INPUT Name WRITEFILE "output.txt", Name NEXT i CLOSEFILE "output.txt" OUTPUT "File saved successfully."
Appending to a File
If you want to add new records to an existing file without losing the old data, use APPEND mode instead of WRITE.
DECLARE NewRecord : STRING // APPEND preserves existing file contents OPENFILE "log.txt" FOR APPEND OUTPUT "Enter new log entry:" INPUT NewRecord WRITEFILE "log.txt", NewRecord CLOSEFILE "log.txt" OUTPUT "Entry added to log."
Using EOF() to Read an Entire File
The EOF(filename) function returns TRUE when there are no more lines to read. Always check this inside a WHILE loop when reading files of unknown length.
WHILE NOT EOF() when the file length is unknown.
Complete Example: Student Score File
This complete example writes 5 student names to a file, then reads them back and displays each one:
DECLARE Name : STRING DECLARE i : INTEGER // PART 1: Write 5 student names to file OPENFILE "class.txt" FOR WRITE FOR i โ 1 TO 5 OUTPUT "Enter student name:" INPUT Name WRITEFILE "class.txt", Name NEXT i CLOSEFILE "class.txt" // PART 2: Read all names back and display them OUTPUT "--- Class Register ---" OPENFILE "class.txt" FOR READ WHILE NOT EOF("class.txt") READFILE "class.txt", Name OUTPUT Name ENDWHILE CLOSEFILE "class.txt"