Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleRead fixed-length records from a file
Keywordsfixed-length, fixed length, record, random access, file
CategoriesFiles and Directories, Software Engineering
 
Open the file for random access and read each record.

Note that the input variable file_record should have the same length as the file records. This variable can be a single fixed length string as in this example, or a user-defined type created using a Type statement.

 
Private Sub Form_Load()
Const RECORD_LENGTH = 10

Dim fnum As Integer
Dim file_name As String
Dim file_record As String * RECORD_LENGTH
Dim i As Integer

    ' Get the file name.
    file_name = App.Path
    If Right$(file_name, 1) <> "\" Then file_name = _
        file_name & "\"
    file_name = file_name & "Fruit.txt"

    ' Open the file.
    fnum = FreeFile
    Open file_name For Random As #fnum Len = RECORD_LENGTH

    ' Read the records.
    For i = 1 To LOF(fnum) \ RECORD_LENGTH
        Get #fnum, i, file_record
        Debug.Print "[" & file_record & "]"
        List1.AddItem Trim$(file_record)
    Next i

    ' Close the file.
    Close #fnum
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated