You are on page 1of 15

Lecture   9: Basic Input and Output

Methods for VB
.NET
Introduction
 So far, we have relied on GUI-based Input/Output…
 By using VB .NET controls…
 For Data Input to our programs:
 = Data Entry
 TextBox (our basic input tool, so far)
 CheckBox, OptionBox, etc (later)
 For Data Output from our programs:
 = Data Displaying
 Label (our basic output tool)
 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
 The 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 .NET provides a number of pre-defined methods for File Handling.
 The 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 scope).
 FileOpen() opens a file, according to mode open_mode…
 This must be done before reading or writing.
 Ex: file_name = “myfile.txt”
 Every open file must be assigned a unique integer file number.
 Ex: file_number = 1
 FileClose( ) closes the file, after the processing job is finished,.
Modes of File Handling
 Opening Files for Sequential Access
 Reading/Writing one stored text data piece at a time…
 Usually, elements are separated by commas (comma-delimited).

 Input Mode (OpenMode.Input)


 Used to input data from the file start.
 Output Mode (OpenMode.Output) Here, we focus on
 Used to output data, to the file start. the basics:
 Append Mode (OpenMode.Append) sequential access.
 Used to update a file (add and save).
 Add sequentially, from the file end.

 However, note that also have random modes of Handling Files:


 Binary Mode (OpenMode.Binary)
 Used to (sequentially or randomly) input and output data in binary format.
 Random Mode (OpenMode.Random)
 Used to read/write data (records) in a random access sequence.
Output Mode
 Output Mode is used to write data sequentially to a file:
1. Open the file, specifying Output Mode via OpenMode.Output;
 In this mode, if the file does not exist, FileOpen() creates it.
 Also, if the file already exists, FileOpen() will destroy all data.
2. Process the file (write data to it) in sequence…
 From the file beginning to the file end.
 Using pre-defined Methods for writing.
3. Close the File.
 The overall syntax is as follows:

FileOpen( file_number, file_name, OpenMode.Output )


File operations ( via subroutines used for writing to the file):
Print ( file_number, saved_data ) …or…
PrintLine ( file_number, saved_data ) …or…

Write ( file_number, saved_data ) …or…


WriteLine ( file_number, saved_data ) …or…

FileClose( file_number )
 Here, saved_data is the single piece of data (text) to save.
Example: Writing to a File
Example: Writing to a File (cont.)
Methods for Writing to a File
 Print( file_num, param_array ) –formatted output to fileName
 param_array is 0 or more comma-separated Objects for formatted output.

 Write( file_num, param_array ) – comma-delimited, no-format output

 PrintLine( file_num, param_array ) – formatted output + newline.

 WriteLine( file_num, param_array ) – comma-delimited output +


newline
Input Mode
 Input Mode is used to read data sequentially from a file:
1. Open the file, specifying Input Mode via OpenMode.Input;
 In this mode, if the file does not exist, FileOpen() throws an Error.
2. Then, process the file in sequence…from the beginning of the data.
 Using the Input() subroutine, or LineInput() function.
 Data read in is to be stored in a variable.
 Data in the file cannot be amended while processing ( read-only ).
3. Close the File.
 The overall syntax is as follows:

FileOpen( file_number, file_name, OpenMode.Input )


File operations:
Input ( file_number, var_name ) ‘sends one piece of data to var_name

…or…
var_name = LineInput ( file_number ) ‘reads and returns one file line

FileClose( file_number )
 Here, var_name is a variable used to store input data.
Example: Reading from a File

Here, vbCrLf is a display constant, which requests a new-line.


Input Example (cont.)
 Repeated clicking does not read File 1’s (prev. saved) 2nd line…
 Instead, we just read/display the same line, repeatedly.

 To read the whole file, one data element at a time we add a loop:
 Which loops Until the End Of File 1 is reached (EOF(1) = True).
 During each loop, Input() gets one data element, and hands it to  word
 Note: Input() reads data separated by commas or line-breaks (omitted).

 Note: when repeatedly clicking ‘Open’, these 4 lines will be repeated.


 You may fix this, by clearing the TextBox first, with: txtOne.Text = “”
Input Example: LineInput()
 If we wish to neglect commas, and read in based only on line-
breaks:
 We may use the function, LineInput( )…
variable_name = LineInput( file_number )

 Unlike Input(), it is a Function, so it reads and returns a line…


 Rather than reading and handing the datum directly to the Variable.
 Example:
Append Mode
 The Append Mode is used to write data sequentially to a file’s end:
 i.e., append data to the end of the opened file.
1. Open the file, specifying the Input Mode via OpenMode.Append;
 This time, FileOpen() does not destroy the file’s data.
2. Then, process the file sequentially…
 using Print(), PrintLine(), Write(), or WriteLine()…
 Data is APPENDED to the END of the file.
3. Close the File.
 The overall syntax is similar to the Open Mode:

FileOpen( file_number, file_name, OpenMode.Append )


File operations:
Print ( file_number, saved_data ) …or…
PrintLine ( file_number, saved_data ) …or…

Write ( file_number, saved_data ) …or…


WriteLine ( file_number, saved_data ) …or…
FileClose( file_number )
Ex: Appending Data to a File

Here, vbCrLf has been used to request a new-line + the typed text.
The FreeFile Function
 In VB .NET you must specify the file number, to open a file.
 This file number must differ from that of every other open file.
 You may use the FreeFile() function to automatically:
 Get and Return the number of the next available Free File.

 An Example demonstrating the use of FreeFile() is shown below:

You might also like