首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

vb中二进制文件怎么删除文件尾部的部分数据

2012-01-24 
vb中二进制文件如何删除文件尾部的部分数据如题吧,比如一个大小为100字节的文件,用Binary打开后,如果删除

vb中二进制文件如何删除文件尾部的部分数据
如题吧,比如一个大小为100字节的文件,用Binary打开后,如果删除文件尾部的20个字节,变为大小是80字节的文件?
或者不用Binary打开,其他的解决办法也行,只要能去掉最后的20个字符.谢谢!!
(别说把文件删了再写一遍...)

[解决办法]
可用下面的例子改造一下

'Example by Skuratovich Pavel aka P@Ssword (pas_sword@tut.by)
'It is an example of use of function SetEndOfFile.
Option Explicit

Private Declare Function CreateFile Lib "kernel32 " Alias "CreateFileA " (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function WriteFile Lib "kernel32 " (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As Any) As Long
Private Declare Function SetFilePointer Lib "kernel32 " (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
Private Declare Function SetEndOfFile Lib "kernel32 " (ByVal hFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32 " (ByVal hObject As Long) As Long

Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Const OPEN_ALWAYS = 4
Private Const FILE_BEGIN = 0

Private Sub Form_Load()
Dim hFile As Long
Dim BytesWritten As Long
Dim Path As String

Path = "C:\EOF.txt "

hFile = CreateFile(Path, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_ALWAYS, 0, 0)
If hFile = -1 Then End
WriteFile hFile, ByVal "Very-very cool & long string ", 28, BytesWritten, ByVal 0&
MsgBox "View file 'C:\EOF.txt ', then click OK "
SetFilePointer hFile, 9, 0, FILE_BEGIN
SetEndOfFile hFile
CloseHandle hFile
MsgBox "Now view file ' " & Path & " ' one more time "
Unload Me
End Sub

热点排行