A Level Computer Science Programming Guide/File Handling

Note for VB.NET users:
While the methods used here are still valid, they are generally not used. They have been included in this guide as they are more similar to the Pseudocode file management, hopefully making them easier to understand. Even though it is not practice to use them, they should still be acceptable in an exam.

Opening Text Files
In  you can use the   function to automatically obtain an unused file number, avoiding any problems that could occur if accidentally using the same number for two files. It is good practice to always keep track of your file numbers, as it will help reduce bugs (aka forgetting or using the wrong file number), remove any Magic Numbers and make the code easier to read and understand.

Reading From Text Files
Once a file is opened in 'Read' mode, you can use the following commands to read the data from the files. The Variable should be of data type. This command reads the text file line by line.

The  function can be used in   and   to determine whether the file pointer is at the end of the file. It returns a  Value, and can be useful when implementing loops.

Writing To Text Files
Once a file is opened in 'Write' or 'Append' mode, you can use the following commands to write the data to the files. The Variable should be of data type.

Closing Text Files
You should always remember to close your files. Failure to do so can result in major problems later on when any program tries to open the file, or the program tries to open or edit a different file with the same identifier.