sazr
sazr

Reputation: 25928

Write string to Unicode File

I am using NSIS Unicode version and I am trying to append a string to an existing Unicode file(UTF-16LE).

My Problem: After I write the string to the file then open the file, the string I wrote is just jibberish. I have a feeling that its trying to write an ANSI string to a UTF-16LE file.

How can I write a string to a unicode file?

Function ${prefix}AppendFile 
    # Note: Will automatically create file if it doesn't exist
    # $0 = fName
    # $1 = strToWrite

    Pop $1
    Pop $0

    ClearErrors
    FileOpen  $3 $0 a
    FileSeek  $3 0 END
    FileWrite $3 "$\r$\n"        # write a new line
    FileWrite $3 "$1"
    FileWrite $3 "$\r$\n"        # write an extra line
    FileClose $3                 # close the file

    IfErrors 0 +2
        MessageBox MB_OK "Append Error: $1 $\r$\n$\r$\n$0"
FunctionEnd

Upvotes: 0

Views: 733

Answers (1)

Chris Morgan
Chris Morgan

Reputation: 90782

If you're dealing with UTF-16LE file, you need to use FileWriteUTF16LE, which writes Unicode text, rather than FileWrite, which writes ANSI text.

Upvotes: 1

Related Questions