Showing posts with label File IO. Show all posts
Showing posts with label File IO. Show all posts

Tuesday, April 1, 2008

ATM Project - Part 1

Now that we have learned about File I/O, loops and decisions we can start to work on a real project. We will create an ATM simulation and we will continue adding things to it throughout the rest of this course.

First we have to decide how an ATM works. We know that it will require the following operations:

1) Validation (pin # and account number)
2) Deposit Money
3) Withdraw Money
4) Account Status (balance etc)

We will have a text file that contains the account numbers and pins in it:
acctinfo.txt

123456789
1234
234567890
9876


This is kept in the debug folder. When the program begins,

Tuesday, March 18, 2008

Writing to Files

In order to write to files, you will have to create a StreamWriter. Here is an example:

Dim sw As IO.StreamWriter
sw = IO.File.CreateText("newfilename.txt")
sw.WriteLine("This will go in the new file")
sw.Close()

If you use CreateText as shown above, then the file in quotes will be created and ready for use. If the file existed before you opened it, the file contents will be deleted, even if nothing is writen to the file. If you just want to place the new text at the end of the existing file, you can use AppendText() instead of CreateText().

Another thing you should know, is that the text will not be written to the file unless you Close() the file.

If you simply place the name of the file in quotes without an absolute path, then the file will be saved in the MyDocuments\Visual Studio 2005\Projects\[project name]\[project name]\bin\Debug folder, where [project name] is the name of your project.

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")