一个文件中有一些数据,怎么把这个文件打开后清空,谢谢

解决方案 »

  1.   

    open file for output as #1
      print#1,""
    close #1
      

  2.   

    s 为你定位擦除的位置!! put#1, s, "" 擦除一个字符open file for binary as #1
      put#1, s, ""
    close #1
      

  3.   

    问题是这是一个系统锁定文件,不能删除,用open命令打开后能aPPEND数据,现在想清空这个文件,请各位赐教
      

  4.   

    一般文件的清空(没试过系统锁定文件):
    Open XXX For Output As #1 '用Output 打开会自动清空
    Close 1
      

  5.   

    Open file For Output As #1
         close #1
     如果文件是只读的,就没有用了,还需要先改变文件的只读属性!
                     
      

  6.   

    用api函数SetEndOfFile的写法:
    Option Explicit
    Const FILE_BEGIN = 0
    Const OFS_MAXPATHNAME = 128
    Const OF_CREATE = &H1000
    Const OF_READ = &H0
    Const OF_WRITE = &H1
    Private Type OFSTRUCT
            cBytes As Byte
            fFixedDisk As Byte
            nErrCode As Integer
            Reserved1 As Integer
            Reserved2 As Integer
            szPathName(OFS_MAXPATHNAME) As Byte
    End TypePrivate Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Function SetEndOfFile Lib "kernel32" (ByVal hFile As Long) As Long
    Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As LongPrivate Sub Command1_Click()
        Dim mfile As String
        mfile = "e:\db1.mdb" '改成你的文件路径
        clearFile mfile
    End SubPrivate Sub clearFile(ByVal mfile As String)
        Dim OF As OFSTRUCT
        Dim hFile As Long
        hFile = OpenFile(mfile, OF, OF_READ Or OF_WRITE)
        SetFilePointer hFile, 0, 0, FILE_BEGIN
        SetEndOfFile hFile
        CloseHandle hFile
    End Sub