introduction to programming - Pseudo code

profileShanEn
week_5_lab_5.2.pdf

PT1420: File Access and Visual Basic

Page 1

This lab examines how to work with a file by writing Visual Basic code. Read the following

programming problem prior to completing the lab.

Write and design a simple program that will take 3 pieces of data and write it to a file. Create

variables and set them equal to the appropriate values:

Declare string firstName = “xxx”

Declare string lastName = “xxx”

Declare integer age = your age

Write this information to a file called myRecords.txt. Next, read this information from

the file and print it to the screen.

Step 1: Create a new Visual Basic workspace and add the following code:

Module Module1

Sub Main()

Dim firstName As String = "XXXX"

Dim lastName As String = "XXX"

Dim age As Integer = ####

Dim writer As System.IO.StreamWriter = System.IO.File.CreateText("myRecords.txt")

writer.WriteLine(firstName & " " & lastName & " " & age)

writer.Close()

Dim reader As System.IO.StreamReader = System.IO.File.OpenText("myRecords.txt")

Dim myInfo As String = reader.ReadLine()

Console.WriteLine(myInfo)

PT1420: File Access and Visual Basic

Page 2

reader.Close()

'this causes a pause so you can see your program

Console.Write("Press enter to continue...")

Console.ReadLine()

End Sub

End Module

Your module looks like this:

PT1420: File Access and Visual Basic

Page 3

Step 2: Change the values of the variables to your records. Your module looks like this:

Step 3: Run your program so that it works properly. Your output might look as follows:

PT1420: File Access and Visual Basic

Page 4

Step 4: Locate the .txt file in the bin\debug folder, and view your .txt file. Your output might

look as follows:

PT1420: File Access and Visual Basic

Page 5

Step 5: Submit the Visual Basic code as a compressed (zipped) folder using the following steps:

a. Open Windows Explorer --> Start --> All Programs --> Accessories --> Windows Explorer.

Your Windows Explorer might look as follows:

PT1420: File Access and Visual Basic

Page 6

b. In Windows Explorer, navigate to the folder that contains your project files. Your

Windows Explorer might look as follows:

(If you don't recall you can check in Visual Studio by opening your project, right click

module1.vb file and view the properties. Look at the full path ex.

C:\Users\instructor\Documents\Visual Studio

2010\Projects\myFirstProgram\myFirstProgram\Module1.vb; in this case navigate to

C:\Users\instructor\Documents\Visual Studio 2010\Projects). Your module properties

might look as follows:

PT1420: File Access and Visual Basic

Page 7

c. Right click on your project folder and choose send to --> compressed folder. This creates

a zip file of all your code. Your Windows Explorer might look as follows:

d. Attach the compressed folder you created to your submission. Your Windows Explorer

might look as follows: