Lab 7.4 – Files & Records

 

Goals:

    w Solidify understanding of simple File I/O

    w Use the Immediate Window for debugging

    w Use user-defined types to create structures

    w Use structures to create records

    w Use random file access

 

__1. Review simple file i/o.

          w Start a new VB project naming it prjRAF and naming the

                    form frmRAF.

          w Place two command buttons on the form, cmdPrint and cmdInput,

                    giving the second one a Visible property of False.

          w Give cmdPrint this code:

                        Static mult As Integer

                        Dim intval As Integer

                        Dim i As Integer

                          mult = mult + 1

                          Open App.Path & "\Numbers" For Output As #1

                          For i = 0 To 15

                             Print #1, Str(i)

                             Print #1, mult * i, mult * i * i

                          Next i

                          cmdPrint.Visible = False

                          cmdInput.Visible = True

                          Close #1

          w Give cmdInput this code:

                        Dim strval As String

                        Dim ival As Integer

                        Dim intval As Integer

                        Dim i As Integer

                          Open App.Path & "\Numbers" For Input As #2

                          For i = 0 To 15

                             Line Input #2, strval

                             Input #2, ival

                             Input #2, intval

                             Debug.Print strval, ival, intval

                          Next i

                          cmdPrint.Visible = True

                          cmdInput.Visible = False

                          Close #2

 

__2. Test the program

          w Run the program clicking on each button in turn, several times.

          w Do you see a window with the title Immediate opened just below the

             form?  If not, click on View from the Menu Bar and choose

             Immediate Window.

          w Observe the output produced in the Immediate Window.  Can you

             explain what is going on?

          w What is the purpose of Str(i)?

          w Explain the operation of Line Input #. How does it differ from Input #?

 

__3. Check the file I/O operation

          w Exit the program and minimize the VB environment.

          w Open the Numbers file with Notepad or Wordpad.

          w Are the file contents as you expected?

 

__4. Continue with simple file i/o.

          w Place two more command buttons on the form, cmdWrite and

             cmdInput2, again giving the second one a Visible property of False.

          w Give cmdWrite this code:

                        Static mult As Integer

                        Dim intval As Integer

                        Dim i As Integer

                          mult = mult + 1

                          Open App.Path & "\More" For Output As #1

                          For i = 0 To 15

                             Write #1, Str(i)

                             Write #1, mult * i, mult * i * i

                          Next i

                          cmdWrite.Visible = False

                          cmdInput2.Visible = True

                          Close #1

 

          w And give cmdInput2 this code:

                        Dim strval As String

                        Dim ival As Integer

                        Dim intval As Integer

                        Dim i As Integer

                          Open App.Path & "\More" For Input As #2

                          For i = 0 To 15

                             Line Input #2, strval

                             Input #2, ival

                             Input #2, intval

                             Debug.Print strval, ival, intval

                          Next i

                          cmdWrite.Visible = True

                          cmdInput2.Visible = False

                          Close #2

 

__5. Compare the file I/O operation to the previous one.

          w Again observe the output in the Immediate Window.

          w Does it differ from the previous one?

          w Again look at the contents of the file produced.  Does it differ from

             the previous one?

          w If you observe any differences, can you explain them?

 

__6. Create a structure (record) for use in random file i/o.

          w Place this code in the General Declarations section of your program.

                        Private Type Simple

                           strval As String * 3

                           int As Integer

                        End Type

                        Const numtorun = 3

          w Highlight the word type and press F1. 

             What do you learn about this keyword?

          w Why do you think strval is declared as String * 3 instead of just

              as String?

 

__7. Create and read a random access file.

          w Place one more command buttons on the form, cmdCreateFile.

 

          w Give it this code:

                        Dim recVar As Simple

                        Dim i As Integer

                         Open App.Path & "\simp" For Random As #1 Len =Len(recVar)

                          For i = 1 To numtorun

                             recVar.int = 3 * i

                             recVar.strval = Str(i)

                             Put #1, i, recVar

                          Next i

                          For i = 1 To numtorun

                             Get #1, i, recVar

                             Debug.Print "Record #"; i; " is: "; recVar.strval; " "; recVar.int

                          Next i

                          Close #1

          w Run the program.  Explain what is happening.

          w Change the For loop as follows:

                        For i = numtorun To 1 Step -1

          w How does this affect the running of the program?

 

__8. Some final observations.

          w Explain the purpose of the App.Path found in several places.

          w Are the two Lens found in the last section the same?  Explain.

          w Explain the purpose of Len(recVar) in the last section?

          w What is the value of Len(recVar)?  Calculate it by hand; then write a

              bit of code to verify your answer.

          w In this line of code below,

                             Get #1, i, recVar

              is a value being read from file #1 into the variable i?  Explain.