Write text file in appending (utf-8 encoded) in VB6

前端 未结 3 1255
情话喂你
情话喂你 2020-12-10 04:43

I have to write a textfile in VB6. I need to do it in appending and utf-8 encoded.

I tried two solutions, one with \"TextStream\" and another one with \"ADODB.Stream

3条回答
  •  被撕碎了的回忆
    2020-12-10 05:00

    Actually no need for API call.

    Option Explicit
    
    Sub testAppend()
        
        Dim fileName
        fileName = "C:\Test\test.txt"
        Dim f As Integer
        f = FreeFile(0)
        Open fileName For Binary Access Write As #f
        Seek #f, LOF(f) + 1
        Dim t
        t = "" & ChrW(107) & ChrW(107) & ChrW(107) & ChrW(106) & ChrW(242) & ChrW(242) & ChrW(107) & ChrW(107) & ChrW(107) & ChrW(107) & ChrW(106) & ChrW(108) & ChrW(242) & ChrW(108) & ChrW(107) & ""
        Put #f, , textToBinary(t, "utf-8")
        Close #f
        
    End Sub
    
    Function textToBinary(text, charset) As Byte()
        
        With CreateObject("ADODB.Stream")
            .Open
            .Type = 2 ' adTypeText
            .charset = charset
            .WriteText text
            .Position = 0
            .Type = 1 ' adTypeBinary
            textToBinary = .Read
            .Close
        End With
        
    End Function```
    
    

提交回复
热议问题