Tuesday, March 18, 2008

File I/O [Reading a File]

Reading Data From Text Files:

There are a few rules to follow when trying to read data from a text file. Text files are written using the standard program Notepad or recently Notepad++. They have the extension .txt. So here are the rules to use them in a VB program.

1. Declare a variable that will handle the reading. For the purpose of the examples below:

Dim sr As IO.StreamReader


The StreamReader is part of the IO (input.output) library. This will read a stream of characters coming from the internet or a disk. The text file will be located on the disk drive.

2. Then, using the variable, execute the statement:

sr = IO.File.OpenText( filename )

where filename is the exact name and path of the text file. For the purpose of the course, ALL text files must be in the bin\Debug folder where your project is located. Here is an example path:

My Documents\Visual Studio 2005\Projects\program\program\bin\Debug

That is indeed a long path. Place the text file in that folder so you don't need to type out the whole name each time.

3. Then, read the data lines IN ORDER, the same order they appear in the file.

To place data from the file into a variable, use the ReadLine function available from the StreamReader library. Using the rule about input from the InputBoxes, everything read from a file will be stored as a string. If you need to gather a numeric value from the file, use the appropriate conversion function from the previous section.

strVar = sr.ReadLine 'any string variable
numVar = CDbl( sr.ReadLine ) 'a double type variable

If all the statements in the file have been read, each subsequent ReadLine call will get a value of Nothing by default.

4. Finally, close the file and continue with the program (if anything else is left).

sr.Close()

That is it in terms of gathering data from a text file. Here is a short program from the textbook example. The text file is called "PAYROLL" and contains the following data IN ORDER:

Mike Jones
7.35
35
John Smith
6.75
33

Program:


Private Sub btnCompute_Click(...) Handles btnCompute.Click

Dim sr As IO.StreamReader = IO.File.OpenText("PAYROLL.TXT")
'The file is in the bin subfolder in Debug

Dim name As String
Dim hourlyWage, hoursWorked, salary As Double

name = sr.ReadLine
hourlyWage =
CDbl(sr.ReadLine)
hoursWorked =
CDbl(sr.ReadLine)
salary = hourlyWage * hoursWorked
lstPayroll.Items.Add(name &
" " & FormatCurrency(salary))

name = sr.ReadLine
hourlyWage =
CDbl(sr.ReadLine)
hoursWorked =
CDbl(sr.ReadLine)
sr.Close()

salary = hourlyWage * hoursWorked
lstPayroll.Items.Add(name &
" " & FormatCurrency(salary))

End Sub




We already know how to read from a file, by creating a StreamReader. For example:

Dim sr As IO.StreamReader
sr = IO.File.OpenText("nameOfFile.txt")

We can then read through the file by using a loop:

Do While sr.Peek <> -1
lstBox.Items.Add(sr.ReadLine)
Loop


Imports

Ever get tired of writing ``IO'' in front of everything dealing with files? You can import it at the top of your code and then you will not have to type it ever again:

Imports System.IO
Public Class
Form1

Notice that it goes BEFORE the class declaration of Form1. Now the code for declaring and using a StreamReader changes:

Dim sr As StreamReader
sr = File.OpenText("nameOfFile.txt")

No comments: