Methods for VB
Introduction
So far, we have relied on GUI-based Input/Output
By using VB controls
For Data Input to our programs
For Data Output from our programs:
= Data Entry
TextBox (our basic input tool, so far)
CheckBox, OptionBox, etc
= Data Displaying
Label, message box
ListBox
However, we may also use Files for I/O
These provide a means for long term storage of results.
Using Files for Input / Output
Process of using files for Data I/O is called File Handling
This requires not only methods for reading and writing data
But also methods for Opening and Closing files
VB provides a number of pre-defined methods for File Handling
Syntax for a general file-handling „block‟ is as follows:
FileOpen( Integer file_number , String file_name , open_mode )
Some Data Processing ( e.g., writing to or reading from a file )
FileClose( file_number )
Note: this is not a block in the formal sense (for variable scope)
FileOpen() opens a file, according to mode open_mode
This must be done before reading or writing
Every open file must be assigned a unique file number
Ex: file_name = “myfile.txt”
Ex: file_number = 1
FileClose( ) closes the file, after processing job is finished.
Modes of File Handling
Opening Files for Sequential Access
Reading/Writing one stored text data piece at a time
Input Mode (OpenMode.Input)
Used to output data, to the file start
Append Mode (OpenMode.Append)
Used to input data from file start
Output Mode (OpenMode.Output)
Usually, elements are separated by commas (comma-delimited)
Here, we focus on the basics: sequential access.
Used to update a file (add and save)
Add sequentially, from the file end
However, note that we also have random modes of Handling Files
Binary Mode