7,763
社区成员
发帖
与我相关
我的任务
分享
' 窗体代码:
Option Explicit
Private strFileName As String
Private Sub Form_Load()
'strFileName = "E:\CESHI.TXT"
strFileName = "E:\Temp\TestA.txt"
End Sub
Private Sub Command1_Click()
Dim arrTest(1) As MyType
arrTest(0).banci = 12345
arrTest(0).xingming = "张三李四"
arrTest(1).banci = 789
arrTest(1).xingming = "吕洞宾"
WriteArray strFileName, arrTest
End Sub
Private Sub Command2_Click()
Dim arrTemp(1) As MyType
Dim i As Long
Call ReadArray(strFileName, arrTemp())
Me.Cls
For i = 0& To 1&
' 加个“<”符号,让你可以看到读出来的文本没有“多余的东西”
Me.Print arrTemp(i).banci, arrTemp(i).xingming & "<"
Next
'Text1.Text = S(0).xingming
End Sub
' 标准模块代码:
Option Explicit
Private Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" ( _
ByRef Destination As Any, _
ByRef Source As Any, _
ByVal Length As Long)
Private Declare Function ReadFile Lib "Kernel32" ( _
ByVal hFile As Long, _
ByRef lpBuffer As Any, _
ByVal nNumberOfBytesToRead As Long, _
ByRef lpNumberOfBytesRead As Long, _
ByVal lpOverlapped As Long) As Long
Private Declare Function CloseHandle Lib "Kernel32" ( _
ByVal hObject As Long) As Long
Private Declare Function WriteFile Lib "Kernel32" ( _
ByVal hFile As Long, _
ByRef lpBuffer As Any, _
ByVal nNumberOfBytesToWrite As Long, _
ByRef lpNumberOfBytesWritten As Long, _
ByVal lpOverlapped As Long) As Long
Private 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
Private Declare Function FlushFileBuffers Lib "Kernel32" (ByVal hFile As Long) As Long
Public Type MyType
banci As Integer
xingming As String
End Type
Private Type MyType_IO
banci As Integer
data(9) As Byte
End Type
Private Const GENERIC_WRITE As Long = &H40000000
Private Const GENERIC_READ As Long = &H80000000
Private Const FILE_ATTRIBUTE_NORMAL As Long = &H80
Private Const CREATE_ALWAYS As Long = 2
Private Const OPEN_ALWAYS As Long = 4
Private Const INVALID_HANDLE_VALUE As Long = -1
Public Sub ReadArray(ByVal Fname As String, anArray() As MyType)
Dim arrBuff() As MyType_IO
Dim strTemp As String
Dim fHandle As Long
Dim fSuccess As Long
Dim lBytesRead As Long
Dim BytesToRead As Long
Dim i%, m As Integer
m = UBound(anArray)
ReDim arrBuff(m)
BytesToRead = (m + 1) * LenB(arrBuff(0))
fHandle = CreateFile(Fname, GENERIC_WRITE Or GENERIC_READ, _
0&, 0&, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0&)
If (INVALID_HANDLE_VALUE = fHandle) Then Exit Sub
fSuccess = ReadFile(fHandle, arrBuff(0), BytesToRead, lBytesRead, 0&)
Call CloseHandle(fHandle)
If (fSuccess) Then
For i = 0 To m
anArray(i).banci = arrBuff(i).banci
strTemp = StrConv(arrBuff(i).data(), vbUnicode)
m = InStr(1&, strTemp, vbNullChar)
If (m) Then strTemp = Left$(strTemp, m - 1&)
anArray(i).xingming = strTemp
Next
End If
End Sub
Public Sub WriteArray(Fname As String, anArray() As MyType)
Dim arrBuff() As MyType_IO
Dim arrTemp() As Byte
Dim fHandle As Long
Dim fSuccess As Long
Dim lBytesWritten As Long
Dim BytesToWrite As Long
Dim i%, m As Integer
m = UBound(anArray)
ReDim arrBuff(m)
For i = 0 To m
arrBuff(i).banci = anArray(i).banci
arrTemp() = StrConv(anArray(i).xingming, vbFromUnicode)
m = 1& + UBound(arrTemp())
If (10 < m) Then m = 10 ' 文本限制最多10字节
If (m) Then Call CopyMemory(arrBuff(i).data(0), arrTemp(0), m)
Next
BytesToWrite = i * LenB(arrBuff(0))
fHandle = CreateFile(Fname, GENERIC_WRITE Or GENERIC_READ, _
0&, 0&, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0&)
If (INVALID_HANDLE_VALUE = fHandle) Then Exit Sub
fSuccess = WriteFile(fHandle, arrBuff(0), BytesToWrite, lBytesWritten, 0&)
If (fSuccess) Then Call FlushFileBuffers(fHandle)
Call CloseHandle(fHandle)
End Sub