VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 使用WriteFile 块读写数据
发表评论(0)作者:, 平台:, 阅读:13816, 日期:2000-03-29
使用WriteFile 块读/写数据

概述

Win32 API 提供了OpenFile 和 WriteFile 函数, 可以对二进制的文件进行读/写。 这两个函数使读/写格式化的文件更加灵活。 这里演示了操作大量格式化数组的一种技巧, 一次完成, 无须一个一个地操作。


实现步骤

1 打开一个新的工程。 增加一个 MODULE1.BAS。


2 把下面的代码复制到 MODULE1.BAS。


Public Const GENERIC_WRITE = &H40000000

Public Const GENERIC_READ = &H80000000

Const FILE_ATTRIBUTE_NORMAL = &H80

Const CREATE_ALWAYS = 2

Const OPEN_ALWAYS = 4

Const INVALID_HANDLE_VALUE= -1

Const FILE_NAME = "TEST.DAT" '任意一个文件名称


Type MyType

value As Integer

End Type


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


Private Declare Function CloseHandle Lib "kernel32" ( _

ByVal hObject As Long) As Long


Private 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


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


Declare Function FlushFileBuffers Lib "kernel32" ( _

ByVal hFile As Long) As Long


Sub fillArray(anArray() As MyType)

Dim x As Integer


For x = 0 To UBound(anArray)

anArray(x).value = x

Next x

End Sub


Sub Main()

Dim fHandle As Integer

Dim T(1000) As MyType '定义一个大数组

Dim S(1000) As MyType '定义又一个大数组


fillArray T '填充

writearray FILE_NAME, T '把整个数组写入文件

readArray FILE_NAME, S '读到另一个数组

End Sub


Sub readArray(Fname As String, anArray() As MyType)

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))

'取 Fname 的句柄

fHandle = CreateFile(Fname, GENERIC_WRITE Or GENERIC_READ, _

0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)

'CreateFile 失败的话返回 INVALID_HANDLE_VALUE

If fhandle <> INVALID_HANDLE_VALUE Then

fSuccess = ReadFile(fHandle, anArray(LBound(anArray)), _

BytesToRead, lBytesRead, 0)

'ReadFile 成功后返回一个非 0 值

'关闭文件

fSuccess = CloseHandle(fHandle)

End If

End Sub


Sub writearray(Fname As String, anArray() As MyType)

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))

'取 Fname 的句柄

fHandle = CreateFile(Fname, GENERIC_WRITE Or GENERIC_READ, _

0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)

'CreateFile 失败的话返回 INVALID_HANDLE_VALUE

If fhandle <> INVALID_HANDLE_VALUE Then

fSuccess = WriteFile(fHandle, anArray(LBound(anArray)), _

BytesToWrite, lBytesWritten, 0)

'WriteFile 成功后返回一个非 0 值

If fSuccess <> 0 Then

'刷新文件缓冲, 马上写入数据

fSuccess = FlushFileBuffers(fHandle)

'关闭文件

fSuccess = CloseHandle(fHandle)

End If

End If

End Sub


3 在菜单 “工程”-“工程1 属性”中, 设置“启动对象” 为 “Sub Main”。


4 按 F5 开始运行。 如果没有错误的话, 在程序运行好后, 在您的 VB 系统目录下会发现增加了个 Test.dat 文件, 里面有一堆乱东东