Working with Files – Part 2

Processing a Fixed-Width "Print" File

 

The Line Input # Statement

 

The Line Input # reads a line (up to the carriage return) from a sequential file.  The general format is:

 

            Line Input #<filenumber>, <string variable>

 

·         filenumber refers to the file that was Opened As that number (for Input) in the Open statement

 

·         string variable is a string variable into which the line read from the sequential file will be stored

 

You would typically use Line Input # to read records from a file where the fields are fixed width.  The technique is to use Line Input # to read the entire line (record) into a string variable, then use various VB functions such as Mid$ and Val to break up the record string into individual fields and assign them to variables defined as the appropriate data type.

 

Example – Reading a Fixed-Width "Print" Format File:

 

Recall the fixed-width "print" format version of the employee file shown earlier:

 

      1    1    2    2   3    3    4    4    5    5    6    6

   1...5....0....5....0....5...0....5....0....5....0....5....0....5.   

ANDY ANDERSON        100    PROGRAMMER           3/4/1997  25.00

BILLY BABCOCK        110    SYSTEMS ANALYST      2/16/1996 33.50

CHARLIE CHEESEMAN    100    COMPUTER OPERATOR    3/1/1996  15.00

DARLENE DUNCAN       200    RECEPTIONIST         10/11/199812.75

ERNIE EACHUS         300    MAIL ROOM CLERK      8/19/1997 10.00

 

The revised "full-blown" example is shown below (with new statements in bold):

 

 

  Dim strEmpFileName As String

  Dim strBackSlash   As String

  Dim intEmpFileNbr  As Integer

 

  Dim strEmpRecord   As String

  Dim strEmpName    As String

  Dim intDeptNbr    As Integer

  Dim strJobTitle   As String

  Dim dtmHireDate   As Date

  Dim sngHrlyRate   As Single

 

  strBackSlash = IIf (Right$(App.Path, 1) = "\", "", "\")                    

  strEmpFileName = App.Path & strBackSlash & "EMPLOYEE.DAT"

  intEmpFileNbr = FreeFile

 

  Open strEmpFileName For Input As #intEmpFileNbr

 

  Do Until EOF(intEmpFileNbr)

      Line Input #intEmpFileNbr, strEmpRecord  

      strEmpName = Left$(strEmpRecord, 20)

      intDeptNbr = Val(Mid$(strEmpRecord, 21, 4))

      strJobTitle = Mid$(strEmpRecord, 30, 21)

      dtmHireDate = CDate(Mid$(strEmpRecord, 51, 10))

      sngHrlyRate = Val(Mid$(strEmpRecord, 61, 5))

      Print strEmpName; _

            Tab(25); Format$(intDeptNbr, "@@@@"); _

            Tab(35); strJobTitle; _

            Tab(60); Format$(dtmHireDate, "mm/dd/yyyy"); _

            Tab(71); Format$(Format$(sngHrlyRate, "Standard"), "@@@@@@@") 

    Loop

 

  Close #intEmpFileNbr

 

 

 

A description of each of the new statements follows:

 

Line Input #intEmpFileNbr, strEmpRecord  

Reads a line from the employee file and stores it in the variable strEmpRecord

 

strEmpName = Left$(strEmpRecord, 20)

Takes the leftmost 20 bytes of strEmpRecord (in the case of the first record, "ANDY ANDERSON") and stores it in the variable strEmpName

 

intDeptNbr = Val(Mid$(strEmpRecord, 21, 4))

Note the use of nested functions here.  The Mid$ function takes the four bytes starting at position 21 of strEmpRecord resulting in the string " 100" in the case of the first record).  The Val function then converts " 100" to a number and causes 100 to be stored in the variable intDeptNbr.

 

strJobTitle = Mid$(strEmpRecord, 30, 21)

Takes the 21 bytes starting at position 30 of strEmpRecord (in the case of the first record, "PROGRAMMER") and stores it in the variable strJobTitle.

 

dtmHireDate = CDate(Mid$(strEmpRecord, 51, 10))

Note the use of nested functions here.  The Mid$ function takes the 10 bytes starting at position 51 of strEmpRecord resulting in the string "3/4/1997" in the case of the first record).  The CDate function then converts this string into a VB internal Date data type and stores it in the variable dtmHireDate. (The CDate function will convert any string that "looks like" a date into the VB Date data type.  This includes strings like "March 4, 1997".)

 

       sngHrlyRate = Val(Mid$(strEmpRecord, 61, 5))

      Note the use of nested functions here.  The Mid$ function takes the five bytes starting at position 61 of strEmpRecord resulting in the string "25.00" in the case of the first record).  The Val function then converts " 25.00" to a number and causes 25 to be stored in the variable sngHrlyRate.

 

To demonstrate the code above

Run the program and click the "Try It" button. The output should look very similar to that of the previous example.

 

Download the VB project code for the example above here.