Inno Setup replace a string in a UTF-8 file without BOM

别说谁变了你拦得住时间么 提交于 2019-12-01 09:56:00

问题


I need to change some values in a configuration file. The file is UTF-8 without BOM. I need to save it the same way. How do I do it with Inno Setup Unicode edition? Note: This doesn't work, and this doesn't show how to read the file correctly.


回答1:


const
  CP_UTF8 = 65001;

{ ... }
var
  FileName: string;
  S: string;
begin
  FileName := 'test.txt';
  if not LoadStringFromFileInCP(FileName, S, CP_UTF8) then
  begin
    Log('Error loading the file');
  end
    else
  if StringChangeEx(S, 'žluťoučký kůň', 'ďábelské ódy', True) <= 0 then
  begin
    Log('No value was replaced');
  end
    else
  if not SaveStringToFileInCP(FileName, S, CP_UTF8) then
  begin
    Log('Error writing the file');
  end
    else
  begin
    Log('Replacement successful');
  end;
end;

LoadStringFromFileInCP and SaveStringToFileInCP come from:
Inno Setup - Convert array of string to Unicode and back to ANSI

The code needs Unicode version of Inno Setup (the only version as of Inno Setup 6).
For Unicode string literals, your .iss file must be in UTF-8 encoding with BOM.



来源:https://stackoverflow.com/questions/54333050/inno-setup-replace-a-string-in-a-utf-8-file-without-bom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!