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

解决方案 »

  1.   

    可用下面的例子改造一下'Example by Skuratovich Pavel aka P@Ssword ([email protected])
    'It is an example of use of function SetEndOfFile.
    Option ExplicitPrivate 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 LongPrivate 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 = 0Private 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
      

  2.   

    关键点 SetEndOfFile 之前需要移动文件指针位置 ,用 SetFilePointer
      

  3.   

    非常感谢 Tiger_Zhao(VB老鸟) 的回复!
    答案很不错~~谢谢!