文章編號: 257794 - 上次校閱: 2004年7月13日 - 版次: 2.2

如何使用與 Visual Basic 的二進位檔案存取

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。

在此頁中

全部展開 | 全部摺疊

結論

本文主要是作為至 MSDN 說明中 < 使用二進位檔案存取 > 主題補充。它提供程式設計範例,該主題中討論的結構為基礎。範例程式碼會示範如何存取是由固定長度記錄使用隨機存取方法組成兩個檔案,檔案組成使用二進位存取方法的可變長度資料錄。

其他相關資訊

若要建立這個範例的步驟:

  1. 啟動新的 Visual Basic 標準 EXE 專案。預設會建立 Form1。
  2. 在 [專案] 功能表上, 選取 [加入模組],以將標準模組加入至專案]。依預設建立 Module1。
  3. 在 [專案] 功能表中,選取 Project1 內容],以顯示 [專案屬性] 對話方塊。選取 [一般] 索引標籤,並將啟始物件變更為 Sub Main
  4. 將下列程式碼加入 Module1 的一般宣告區段:
    Option Explicit
    ' define structure of a variable-length record
    Type Person_VariableLength
        ID               As Integer
        MonthlySalary    As Currency
        LastReviewDate   As Long
        FirstName        As String
        LastName         As String
        Title            As String
        ReviewComments   As String
    End Type
    
    ' define structure of a fixed-length record
    Type Person_FixedLength
        ID               As Integer
        MonthlySalary    As Currency
        LastReviewDate   As Long
        FirstName        As String * 15
        LastName         As String * 15
        Title            As String * 15
        ReviewComments   As String * 150
    End Type
    
    ' variables for sample data
    Private binSampleOne As Person_VariableLength
    Private binSampleTwo As Person_VariableLength
    Private randomSampleOne As Person_FixedLength
    Private randomSampleTwo As Person_FixedLength
    
    ' record number for use with random (fixed-length) file
    Private intRecNum As Integer
    
    Public Sub main()
        buildSampleRecords
        demonstrateBinary
        demonstrateRandom
    End Sub
    
    Private Function demonstrateBinary()
        ' open the file and write the records
        Open "c:\BINsample" For Binary As #1
        Put #1, , binSampleOne
        Put #1, , binSampleTwo
        Close #1
        
        ' open the file and read the records
        Open "c:\binSample" For Binary As #1
        Dim udtPerson As Person_VariableLength
        Do While Not EOF(1)
            Get #1, , udtPerson
            ' display selected values
            With udtPerson
                Debug.Print .FirstName
                Debug.Print .LastName
                Debug.Print .ReviewComments
            End With
        Loop
        Close #1
    End Function
    
    Private Function demonstrateRandom()
        ' open the file and write the records
        Dim recSize As Person_FixedLength
        Open "c:\Randomsample" For Random As #1 Len = Len(recSize)
        Put #1, 1, randomSampleOne
        Put #1, 2, randomSampleTwo
        Close #1
        
        'open the file and read the records
        Dim udtPerson As Person_FixedLength
        Open "c:\randomsample" For Random As #1 Len = Len(udtPerson)
        ' demonstrate random access capability by reading file backwards
        For intRecNum = 2 To 1 Step -1
            Get #1, intRecNum, udtPerson
            ' display selected values
            With udtPerson
                Debug.Print .FirstName
                Debug.Print .LastName
                Debug.Print .ReviewComments
            End With
        Next intRecNum
        Close #1
    End Function
    
    Private Sub buildSampleRecords()
    ' routine to build sample records
    ' note that populating the data of the User Defined Type is the
    ' same regardless of fixed or variable length members
       With binSampleOne
            .ID = 1
            .MonthlySalary = 50000
            .LastReviewDate = #2/2/2000#
            .FirstName = "Wilma"
            .LastName = "Flintstone"
            .Title = "Movie Star"
            .ReviewComments = "Contract renewed for sequel"
        End With
        With binSampleTwo
            .ID = 2
            .MonthlySalary = 50000
            .LastReviewDate = #2/2/2000#
            .FirstName = "Fred"
            .LastName = "Flintstone"
            .Title = "Movie Star"
            .ReviewComments = "Replace with Clint Eastwood for sequel"
        End With
        With randomSampleOne
            .ID = 1
            .MonthlySalary = 50000
            .LastReviewDate = #2/2/2000#
            .FirstName = "Wilma"
            .LastName = "Flintstone"
            .Title = "Movie Star"
            .ReviewComments = "Contract renewed for sequel"
        End With
        With randomSampleTwo
            .ID = 2
            .MonthlySalary = 50000
            .LastReviewDate = #2/2/2000#
            .FirstName = "Fred"
            .LastName = "Flintstone"
            .Title = "Movie Star"
            .ReviewComments = "Replace with Clint Eastwood for sequel"
        End With
    End Sub
    					
  5. 請確定顯示 [即時運算] 視窗。如果不是,您需要從 [檢視] 功能表選取。
  6. 執行該專案,並注意結果會顯示在即時運算視窗] 中。

?考

MSDN 說明主題: 「 使用二進位檔案存取 」 會在下列網站:

http://msdn.microsoft.com/en-us/library/aa231223(VS.60).aspx (http://msdn.microsoft.com/en-us/library/aa231223(VS.60).aspx)

這篇文章中的資訊適用於:
  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 6.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
關鍵字:?
kbmt kbfileio kbhowto kbtophit KB257794 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:257794? (http://support.microsoft.com/kb/257794/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。