8.10 - File Handling
What is file handling?
File handling allows a program to interact with data stored in external files, such as reading existing information or writing new data. This makes programs more versatile, as they can access and modify persistent data outside the program's immediate memory.
Types of text files
The focus here is on handling text files, which store plain text data. Common types include:
- .txt - Basic text files for simple data storage
- .csv - Comma-separated values files, often used for structured data like spreadsheets
- .dat - General data files that can hold various text-based information
Opening and creating external files
Before a program can read from or write to an external file, it must first open the file. This establishes a connection between the program and the file, allowing data access.
Opening an existing file
Use the open() command to access an existing file, assigning it to a variable for reference. The command requires the file name, and sometimes the full file path if the file is not in the current directory.
Some programming languages require specifying a mode when opening a file, such as read mode for viewing data or write mode for adding information.
Creating a new file
To create a new text file, use the newFile() command with the desired file name. After creation, you must still open the file using open() to work with it.
This process ensures the file exists and is ready for reading or writing operations.
Writing data to files
Once a file is open, you can add data to it using write commands.
Using the writeLine() command
The writeLine() command adds a new line of text to the end of the file. It is called on the variable that holds the open file.
For example, consider a program that writes names from an array to a file:
This code produces a file with content like:
- 0 Alice
- 1 Bob
- 2 Charlie
Each writeLine() call automatically adds the text as a new line at the file's end, making it useful for logging or saving sequential data.
Reading data from files
Reading allows a program to retrieve data from an open file, starting from the beginning and progressing line by line.
Using the readLine() command
The readLine() command reads one line of text at a time, keeping track of the current position (often called a cursor) in the file. After reading a line, the cursor moves to the start of the next line.
For example:
If the file contains:
- 0 Alice
- 1 Bob
- 2 Charlie
Then firstLine would be "0 Alice" and secondLine would be "1 Bob". This method is ideal for processing files sequentially, such as loading data into an array.
Checking for the end of a file
When reading files of unknown length, it's important to detect when all data has been read to avoid errors.
Using the endOfFile() command
The endOfFile() command returns TRUE if the cursor has reached the end of the file, and FALSE otherwise. It is commonly used in loops to read until no more data remains.
For example, to read all lines into an array:
This would output: ["0 Alice", "1 Bob", "2 Charlie"]
The loop continues reading lines until endOfFile() returns TRUE, ensuring the program processes the entire file without going beyond its end.
Closing files after use
After completing read or write operations, always close the file to release system resources and prevent issues.
Using the close() command
The close() command is called on the file variable to end the connection. Forgetting to close a file can lock it, stopping other programs or users from accessing it.
For example, in the earlier code snippets, winners.close() ensures the file is properly closed after use.
Why closing is important
- Resource management - Frees up memory and file handles for other tasks
- Data integrity - Ensures all changes are saved and the file is not left in an inconsistent state
- Accessibility - Allows other programs to open and edit the file without conflicts
Always include a close command at the end of file handling routines to maintain good programming practice.