API方式读写文本文件

API方式读写文本文件,第1张

概述API方式读写文本文件

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

Option ExplicitPrivate Const OFS_MAXPATHname = 128Private Type OFSTRUCT    cBytes As Byte    fFixeddisk As Byte    nErrCode As Integer    Reserved1 As Integer    Reserved2 As Integer    szPathname(OFS_MAXPATHname) As ByteEnd TypePrivate 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 LongPrivate Declare Function Openfile lib "kernel32" (ByVal lpfilename As String,lpReOpenBuff As OFSTRUCT,ByVal wStyle As Long) As LongPrivate Declare Function SetfilePointer lib "kernel32" (ByVal hfile As Long,ByVal ldistancetoMove As Long,lpdistancetoMoveHigh As Long,ByVal DWMoveMethod As Long) As LongPrivate Declare Function GetfileSize lib "kernel32" (ByVal hfile As Long,lpfileSizeHigh As Long) As LongPrivate Declare Function Readfile lib "kernel32" (ByVal hfile As Long,lpBuffer As Any,ByVal nNumberOfBytesToRead As Long,lpNumberOfBytesRead As Long,ByVal lpOverlapped As Any) As LongPrivate Declare Function Writefile lib "kernel32" (ByVal hfile As Long,ByVal nNumberOfBytesToWrite As Long,lpNumberOfBytesWritten As Long,lpOverlapped As Any) As LongPrivate Declare Function CloseHandle lib "kernel32" (ByVal hObject As Long) As LongPrivate Const OF_READ = &H0Private Const OF_WRITE = &H1Private Const file_BEGIN = 0Private Const file_END = 2Private Const file_SHARE_READ = &H1Private Const file_SHARE_WRITE = &H2Private Const CREATE_NEW = 1Private Const CREATE_ALWAYS = 2Private Const OPEN_EXISTING = 3Private Const OPEN_ALWAYS = 4Private Const GENERIC_READ = &H80000000Private 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 = retBEnd 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 = retuEnd 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 = retBEnd 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 = retuEnd Function

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的API方式读写文本文件全部内容,希望文章能够帮你解决API方式读写文本文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1274816.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存