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

前端 未结 3 1239
情话喂你
情话喂你 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 = "<tag>" & 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) & "</tag>"
        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```
    
    
    0 讨论(0)
  • 2020-12-10 05:04

    I prefer to save it ANSI as it does by default. Open it with a notepad and overwrite it selecting UTF8 encoding. I found it's the fastest way by far. And I use some other code to append, for example for a database convertion:

    Dim fs As Object, a
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set a = fs.CreateTextFile(filename, True) 'example (myfile.xml, True)
    a.writeline var1
    a.writeline var2
    a.Close
    
    0 讨论(0)
  • 2020-12-10 05:20

    You could combine binary I/O with an API call to perform the conversion to UTF-8:

    Option Explicit
    
    Private Const CP_UTF8 As Long = 65001
    
    Private Declare Function WideCharToMultiByte Lib "kernel32" ( _
        ByVal CodePage As Long, _
        ByVal dwFlags As Long, _
        ByVal lpWideCharStr As Long, _
        ByVal cchWideChar As Long, _
        ByVal lpMultiByteStr As Long, _
        ByVal cchMultiByte As Long, _
        ByVal lpDefaultChar As Long, _
        ByVal lpUsedDefaultChar As Long) As Long
    
    Private Function OpenAppendUTF8(ByVal FileName As String) As Integer
        OpenAppendUTF8 = FreeFile(0)
        Open FileName For Binary Access Write As #OpenAppendUTF8
        Seek #OpenAppendUTF8, LOF(OpenAppendUTF8) + 1
    End Function
    
    Private Sub WriteUTF8( _
        ByVal FNum As Integer, _
        ByVal Text As String, _
        Optional ByVal NL As Boolean)
    
        Dim lngResult As Long
        Dim UTF8() As Byte
    
        If NL Then Text = Text & vbNewLine
        lngResult = WideCharToMultiByte(CP_UTF8, 0, StrPtr(Text), Len(Text), _
                                        0, 0, 0, 0)
        If lngResult > 0 Then
            ReDim UTF8(lngResult - 1)
            WideCharToMultiByte CP_UTF8, 0, StrPtr(Text), Len(Text), _
                                VarPtr(UTF8(0)), lngResult, 0, 0
            Put #FNum, , UTF8
        End If
    End Sub
    
    Private Sub Main()
        Dim F As Integer
    
        F = OpenAppendUTF8("test.txt")
        WriteUTF8 F, "Hello"
        WriteUTF8 F, ChrW$(&H2026&)
        WriteUTF8 F, "World", True
        Close #F
        MsgBox "Done"
    End Sub
    
    0 讨论(0)
提交回复
热议问题