加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

API方式读写文本文件

发布时间:2020-12-17 07:59:40 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 Option ExplicitPrivate Const OFS_MAXPATHNAME = 128Private Type OFSTRUCT cBytes As Byte fFixedDisk As Byte nErrCode As Integer Reserved1 As I

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

Option Explicit


Private Const OFS_MAXPATHNAME = 128

Private Type OFSTRUCT
    cBytes As Byte
    fFixedDisk As Byte
    nErrCode As Integer
    Reserved1 As Integer
    Reserved2 As Integer
    szPathName(OFS_MAXPATHNAME) As Byte
End Type

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 OpenFile Lib "kernel32" (ByVal lpFileName As String,lpReOpenBuff As OFSTRUCT,ByVal wStyle 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 Long
Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long,lpFileSizeHigh As Long) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long,lpBuffer As Any,ByVal nNumberOfBytesToRead As Long,lpNumberOfBytesRead As Long,ByVal lpOverlapped As Any) As Long
Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long,ByVal nNumberOfBytesToWrite As Long,lpNumberOfBytesWritten As Long,lpOverlapped As Any) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Const OF_READ = &H0
Private Const OF_WRITE = &H1
Private Const FILE_BEGIN = 0
Private Const FILE_END = 2
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Const CREATE_NEW = 1
Private Const CREATE_ALWAYS = 2
Private Const OPEN_EXISTING = 3
Private Const OPEN_ALWAYS = 4
Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000



'将Byte数组写入到文件
Public Function WriteBytesToFile(ByVal filePath As String,ByRef bBytes() As Byte) As Boolean
    Dim fHandle As Long
    Dim OF As OFSTRUCT,retB As Boolean
    Dim nSize As Long,ret As Long
    retB = False
    nSize = UBound(bBytes)
    fHandle = CreateFile(filePath,GENERIC_WRITE,FILE_SHARE_READ Or FILE_SHARE_WRITE,ByVal 0&,OPEN_ALWAYS,0)
    If fHandle <> -1 Then
        SetFilePointer fHandle,FILE_BEGIN
        WriteFile fHandle,bBytes(0),nSize + 1,ret,ByVal 0&
        CloseHandle fHandle
        retB = True
    End If
    WriteBytesToFile = retB
End Function


'将二进制文件读入到 Byte数组
Public Function ReadFileToBytes(ByVal filePath As String,ByRef bBytes() As Byte) As Boolean
    On Error Resume Next
    Dim fHandle As Long
    Dim OF As OFSTRUCT,retu As Boolean
    Dim nSize As Long,ret As Long
    retu = False
    fHandle = OpenFile(filePath,OF,OF_READ)
    If fHandle <> -1 Then
        nSize = GetFileSize(fHandle,0)
        If nSize > 0 Then
            ReDim bBytes(nSize - 1) As Byte
            SetFilePointer fHandle,FILE_BEGIN
            ReadFile fHandle,nSize,ByVal 0&
            retu = True
        End If
        CloseHandle fHandle
    End If
    If Err Then
        Err.Clear
        retu = False
    End If
    ReadFileToBytes = retu
End Function


'API方式写文本文件
Public Function SaveToTextFile(filePath As String,content As String,saveMode As Byte) As Boolean
    Dim i As Long,L As Long,bBytes(1) As Byte,ascValue As Long
    Dim fHandle As Long,ret As Long,retB As Boolean
    retB = False
    
    '检查参数
    If Len(filePath) = 0 Or Len(content) = 0 Then
        SaveToTextFile = False
        Exit Function
    End If
    
    '打开文件
    If saveMode = 2 Then
    fHandle = CreateFile(filePath,CREATE_ALWAYS,0)
    Else
    fHandle = CreateFile(filePath,0)
    End If
    If fHandle <> -1 Then
        If saveMode = 2 Then
        SetFilePointer fHandle,FILE_BEGIN
        Else
        SetFilePointer fHandle,FILE_END
        End If
        L = Len(content)
        For i = 1 To L
            ascValue = Asc(Mid(content,i,1))
            bBytes(0) = ascValue And &HFF
            bBytes(1) = (ascValue And &HFF00&)  256
            If bBytes(1) > 0 Then
                WriteFile fHandle,bBytes(1),1,ByVal 0&
                WriteFile fHandle,ByVal 0&
            Else
                WriteFile fHandle,ByVal 0&
            End If
        Next
        CloseHandle fHandle
        retB = True
    End If
    SaveToTextFile = retB
End Function



'API方式读文本文件
Public Function ReadFromTextFile(ByVal filePath As String,ByRef content As String) As Boolean
    On Error Resume Next
    Dim fHandle As Long,bBytes() As Byte
    Dim OF As OFSTRUCT,sFile As String
    Dim nSize As Long,retu As Boolean
    
    retu = False
    sFile = filePath
    fHandle = OpenFile(sFile,OF_READ)
    
    If fHandle <> -1 Then
        nSize = GetFileSize(fHandle,ByVal 0&
            content = StrConv(bBytes(),vbUnicode)
            retu = True
        End If
        CloseHandle fHandle
    End If
    
    If Err Then
        Err.Clear
        retu = False
    End If
    ReadFromTextFile = retu
End Function

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读