Please run the code below. The second function, FileAppend_EncodingEmptyString(), produces a broken character. The function passes an empty string to the third parameter of FileAppend. I expected that it uses the same value as A_FileEncoding when an empty string is passed. But it seems to use the system default's encoding as the documentation for FileEncoding says as below
It also says,One of the following values (if omitted, it defaults to the system default ANSI code page, which is also the default setting):
But it doesn't apply to a case that the parameter is omitted seen in FileAppend_EncodingOmitted(). So the documentation doesn't explain why characters get broken with an empty string parameter value.The default encoding is not used if a UTF-8 or UTF-16 byte order mark is present in the file, unless the file is being opened with write-only access (i.e. the previous contents of the file are being discarded).
Code: Select all
FileEncoding, UTF-8
sPath := A_ScriptDir "\test-01.txt"
For _i, sFuncName in [ "FileAppend_EncodingOmitted", "FileAppend_EncodingEmptyString", "FileAPpend_EncodingSpecified" ] {
FileDelete, % sPath
FileAppend, % "✔️", % sPath
FileRead, sRaw, % sPath
sModified := StrReplace(sRaw, "✔️", "✔️ Modified")
%sFuncName%(sModified, sPath) ; performs FileAppend
FileRead, sResult, % sPath
sExpected := "✔️ Modified"
bPassed := sExpected == sResult
msgbox, % bPassed ? "" : 16, % sFuncName ": " (bPassed ? "Passed" : "Failed"), % ""
. "Expected: " sExpected "`n"
. "Actual: " sResult "`n"
}
FileAppend_EncodingOmitted(sText, sPathFile) {
FileDelete, % sPathFile
FileAppend, % sText, % sPathFile
}
FileAppend_EncodingEmptyString(sText, sPathFile, sEncoding:="") {
FileDelete, % sPathFile
FileAppend, % sText, % sPathFile, % sEncoding
}
FileAppend_EncodingSpecified(sText, sPathFile, sEncoding:="") {
FileDelete, % sPathFile
FileAppend, % sText, % sPathFile, % sEncoding ? sEncoding : A_FileEncoding
}