CSC106 Intermediate PC Applications and Programming
Visual Basic Input Files


In all of our programs so far, the input data was entered by the user into a form when the program is executing. This is fine for certain programs, but there are other programs that get their input from files stored on the computer. Often when data is read from a file it is done so because there is too much for the user to enter in a form.

Creating an Input File

If a program is going to read data from a file, then the file must exist on disk before the program is executed. To create the data file, use textpad. Type in one value on each line of the file. It is very important to make sure that there are no blank lines at the end of the data. Press the down arrow repeatedly and make sure that it stops at the line after the last number. If it doesn't stop there, delete the blank lines at the bottom until it does.

The data file must be saved in the folder <project folder>\bin\debug, where <project folder> is the folder in which the project is saved. When saving the file, use .txt as the type of the file.

Using a File

To read from a file in a program, several steps are required:

  1. the file must be declared, or named inside your program, and opened, or prepared for use
  2. the data in the file must be read into a variable or variables so it can be accessed by program statements
  3. when the program is done reading from the file, it must be closed

Declaring and Opening a File

To declare a file you must write a dim statement for type IO.StreamReader. To open a file, use the Open statement. Usually we combine these into one statement:

   Dim srname As IO.StreamReader = IO.File.OpenText("filename.txt")

where srname is a name you choose to use for the file in the program and filename is the name of the file we created in textpad.

So, for example, if we want to declare the name sr for the file and open the file created with textpad named "grades.txt" we use the statements:

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

If we get the name of the file created with textpad from a TextBox, we will create filename using the statements:

   Dim sr As IO.StreamReader = IO.File.OpenText(textboxname.Text)

So, for example, if we want to declare the name sr for the file and open a file whose name is in the TextBox txtFname, we use the statements:

   Dim sr As IO.StreamReader = IO.File.OpenText(txtFname.Text)

Reading Data From a File

Once a file is opened, the data can be read. Each number in the file is read into a variable using ReadLine. The first ReadLine reads the first number in the file, the second ReadLine reads the second number in the file, etc. Each ReadLine is assigned to a variable; the value read from the file will be stored in this variable. If the value is being stored in an Integer, the statement will be:

variable = CInt(sr.ReadLine)

where variable is the name of the variable that will get the value read from the file. After this statement is executed, variable will contain the number that was read from the file. For example, suppose the file example.txt has the number 7 on the first line and the number 25 on the second line. Then:

   Dim num1 As Integer
   Dim num2 As Integer
   Dim sr As IO.StreamReader = IO.File.OpenText("example.txt")
   num1 = CInt(sr.ReadLine)
   num2 = CInt(sr.ReadLine)

After these statements, the variable num1 will contain the value 7, and the variable num2 will contain the value 25.

If the value being read is a real number and is being stored in a Double, the statement will be:

variable = CDbl(sr.ReadLine)

where variable is the name of the variable that will get the value read from the file. If the value being read is made up of characters and is being stored in a String, the statement will be:

variable = sr.ReadLine

where variable is the name of the variable that will get the value read from the file.

Reading All the Data From a File

To read all the data in a file, we use a loop. We stop the loop when all the data has been read. To check whether there is any data left to read, we use Peek. When there is no more data, the value of Peek is -1. Therefore we can keep looping while Peek is not -1. The condition is written:

   Do While sr.Peek <> -1

For example:

   Dim num As Integer
   Dim sr As IO.StreamReader = IO.File.OpenText("example.txt")
   Do While sr.Peek <> -1
      num = CInt(sr.ReadLine)
      ' do some other stuff in the loop
   Loop

Closing a File

When the program is has read the data from a file, the file should be closed. The Close statement uses the same name as the Open statement:

   sr.Close()


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2007