Dim getValue() As BytePrivate Sub Command1_Click()
Open "C:\1.cmd" For Binary Access Write As #2
     Put #2, , getValue()
Close #2End SubPrivate Sub Form_Load()Open "C:\command.com" For Binary Access Read As #1
      ReDim getValue(FileLen("C:\command.com"))
      Get #1, , getValue
Close #1
End Sub

解决方案 »

  1.   

    楼上这位 Cooly(Lazy) ,谢谢你,但是你显然误解了我的本意我的问题不在于如何读、写二进制文件,而是使用什么样的数据类型才能在读写大容量二进制文件时耗时少一点,编程方便一点比如有这样的记录,格式如下: 
    Field1  日期
    Field2  时间
    Field3  名称
    Field4  单位
    Field5  值如果有类似这样的记录 Max(Double)条,如何快速读写啊,注意了,需要快速读写
      

  2.   

    模块:Option ExplicitPublic Const GENERIC_WRITE = &H40000000
    Public Const GENERIC_READ = &H80000000
    Public Const FILE_ATTRIBUTE_NORMAL = &H80
    Public Const CREATE_ALWAYS = 2
    Public Const OPEN_ALWAYS = 4
    Public Const INVALID_HANDLE_VALUE = -1
    Public Const FILE_NAME = "TEST.DAT" 'Public Declare Function WinExec Lib "kernel32" (ByVal lpCmdLine As String, ByVal nCmdShow As Long) As Long
    Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
    Public Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long
    Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Public Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long
    Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
    Public Declare Function FlushFileBuffers Lib "kernel32" (ByVal hFile As Long) As Long源文件:
    Option ExplicitSub Main1()
        Dim fHandle As Integer
        Dim T(1000) As Byte
        Dim S(1000) As Byte
     '   fillArray T
     '   writearray FILE_NAME, T
     '   readArray FILE_NAME, S
    End SubSub readArray(Fname As String, anArray() As Byte)
        
        Dim fHandle As Long
        Dim fSuccess As Long
        Dim sTest As String
        Dim lBytesRead As Long
        Dim BytesToRead As Long    BytesToRead = (UBound(anArray) + 1) * LenB(anArray(0))
        fHandle = CreateFile(Fname, GENERIC_WRITE Or GENERIC_READ, _
                0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
        If fHandle <> INVALID_HANDLE_VALUE Then
            fSuccess = ReadFile(fHandle, anArray(LBound(anArray)), _
            BytesToRead, lBytesRead, 0)
            fSuccess = CloseHandle(fHandle)
        End IfEnd SubSub writearray(Fname As String, anArray() As Byte)
        
        Dim fHandle As Long
        Dim fSuccess As Long
        Dim sTest As String
        Dim lBytesWritten As Long
        Dim BytesToWrite As Long    BytesToWrite = (UBound(anArray) + 1) * LenB(anArray(0)) '计算所有的字节数量
        fHandle = CreateFile(Fname, GENERIC_WRITE Or GENERIC_READ, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
        If fHandle <> INVALID_HANDLE_VALUE Then
            fSuccess = WriteFile(fHandle, anArray(LBound(anArray)), _
            BytesToWrite, lBytesWritten, 0)
            If fSuccess <> 0 Then
                fSuccess = FlushFileBuffers(fHandle)
                fSuccess = CloseHandle(fHandle)
            End If
        End If
        
    End SubPrivate Sub Command1_Click()
    Dim str() As Byte
    str = StrConv("test", vbFromUnicode)
    writearray "d:\zjl.txt", str
    End Sub
      

  3.   

    转换的问题怕是不太好办,如果这部分这么要求效率,建议用C写一个DLL进行调用
      

  4.   

    用自定义类型:
    Type MyData
       mDate as Date
       mTime as Date
       mValue1 as Single
       mValue2 as Single
       mValue3 as Long
    End TypeDim WriteArray() as mData
    写时可以将所有元素赋值后,用Put #hFile,,WriteArray()一次写入
    读时根据自定义类型长度和文件长度计算出数组大小,然后定义相应的数组,
    用Get #hFile,,ReadArray()一次读入。