File Handling
Note for VB.NET users:
[edit | edit source]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.
Handling Text Files
[edit | edit source]Opening Text Files
[edit | edit source]Language | General Usage | Example Usage |
---|---|---|
Pseudocode | OPENFILE <File Identifier> FOR <File Mode>
|
OPENFILE Beans.txt FOR APPEND
OPENFILE Styles.css FOR READ
OPENFILE Names.tmp FOR WRITE
|
VB.NET | FileOpen(<File Number>], <File Identifier>, OpenMode.<File Mode>)
|
FileOpen(17, “Beans.txt”, OpenMode.Append)
FileOpen(FileNumber, “Styles.css”, OpenMode.Input)
FileOpen(NamesFile, “Names.tmp”, OpenMode.Output)
|
In VB.NET
you can use the FreeFile()
function to automatically obtain an unused file number, avoiding any problems that could occur if accidentally using the same number for two files.
Dim FileNumber As Integer = FreeFile()
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.
File Mode Identifiers | Description | |
---|---|---|
Pseudocode | VB.NET | |
APPEND
|
OpenMode.Append
|
Used when Writing data to the file.
If the file already exists, the new data will be added to the file after any existing data. |
READ
|
OpenMode.Input
|
Used when Reading data from the file. |
Write
|
OpenMode.Output
|
Used when Writing data to the file.
If the file already exists, the file will be deleted and a new file will be created with the new data. |
Reading From Text Files
[edit | edit source]Once a file is opened in 'Read' mode, you can use the following commands to read the data from the files.
Language | General Usage | Example Usage |
---|---|---|
Pseudocode | READFILE <File Identifier>, <Identifier>
|
/Datatypes declared as strings
READFILE Styles.css, NextLine
READFILE TransactionHistory.txt, Transaction
READFILE Address.db, CurrentAddress
WRITEFILE <File identifier>, <Variable>
|
VB.NET | <Identifier> = LineInput(<File Identifier>)
File Identifer is the file number used identify the file when it was opened.
|
NextLine = LineInput(StylesFileNumber)
Transaction = LineInput(TransactionHistoryFileNumber)
CurrentAddress = LineInput(AddressFileNumber)
|
The Variable should be of data type STRING
. This command reads the text file line by line.
The EOF()
function can be used in Pseudocode
and VB.NET
to determine whether the file pointer is at the end of the file. It returns a Boolean
Value, and can be useful when implementing loops.
EOF(<File Identifier>)
Pseudocode | VB.NET |
---|---|
EOF(file.txt)
|
EOF(FileNumber)
|
Writing To Text Files
[edit | edit source]Once a file is opened in 'Write' or 'Append' mode, you can use the following commands to write the data to the files.
Language | General Usage | Example Usage |
---|---|---|
Pseudocode | READFILE <File Identifier>, <Identifier>
|
WRITEFILE Beans.txt, BeanData
WRITEFILE Names.tmp, CurrentUser
WRITEFILE Recipes.db, NewRecipe
|
VB.NET | PrintLine(<File Identifier>, <Identifier>)
File Identifer is the file number used identify the file when it was opened.
|
PrintLine("Beans.txt", BeanData)
PrintLine("Name.tmp", CurrentUser)
PrintLine("Recipes.db" NewRecipe)
|
The Variable should be of data type STRING
.
Closing Text Files
[edit | edit source]Language | General Usage | Example Usage |
---|---|---|
Pseudocode | CLOSEFILE <File identifier>
|
CLOSEFILE Beans.txt
CLOSEFILE Styles.css
CLOSEFILE Names.tmp
|
VB.NET | FileClose(<File Identifier>)
File Identifer is the file number used identify the file when it was opened.
|
FileClose(17)
FileClose(FileNumber)
FileClose(NamesFile)
|
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.