1

I'm trying to open an INI file on Mac OSX using TIniFile:

SettingsFile := TIniFile.Create(aINIFileName);   //Ini vertion
Try
  iUpdateMaj := SettingsFile.ReadInteger('CurrentVersion', 'Maj', 0);
  iUpdateMin := SettingsFile.ReadInteger('CurrentVersion', 'Min', 0);
  iUpdateRel := SettingsFile.ReadInteger('CurrentVersion', 'Rel', 0);
Finally
  SettingsFile.Free;
End;

The exact error I got is:

(EEncodingError) No mapping for the Unicode character exists in the target multi-byte code page.

This was working fine before in Yosemite, but since El Capitan it is no longer working.

From research I've done, the problem is related to Unicode endoding. I know that the INI file created with Notepad is encoded in UTF-8, and TIniFile use TStrings internally.

Does anyone have an idea how I can resolve this issue?

Sebastian Z
  • 4,520
  • 1
  • 15
  • 30
Alain V
  • 311
  • 3
  • 17
  • 2
    Which line *specifically* is failing? Is the problem related to opening an INI file that has a Unicode filename, or reading values from the INI using UTF-8 data? It makes a big difference? What does the call stack look like when the error is thrown? What effort have you done so far to debug this yourself? – Remy Lebeau Sep 23 '16 at 22:00
  • It fail at the .create line, more in depth, it break at:List.Text := FEncoding.GetString(Buffer, Size, Length(Buffer) - Size); line in the TMemIniFile.LoadValues proc in System.IniFiles, In the TEncoding.GetString I can see that the Error is raised because the ByteCount <0 and len =0 but I've no idea why this situation occur. The name of the ini file is RELEASE,INI and it is donwloaded from a HTML Server. – Alain V Sep 24 '16 at 18:14

2 Answers2

3

Your code looks perfectly fine if your .ini file is UTF-8 encoded and contains valid UTF-8 data. I don't know what is inside of your file, but it looks like your .ini file does not contain valid UTF-8 data.

So you'd have to find what encoding your .ini file uses. If your .ini file is ANSI, then you can open it like this:

SettingsFile := TIniFile.Create(aINIFileName, TEncoding.ANSI);
Sebastian Z
  • 4,520
  • 1
  • 15
  • 30
  • I appear that the file is ANSI. By adding TEncoding.ANSI, it's working. Sub question: using editor like Notepad can we force UTF-8 encoding ? I was certain that Notepad was encoding in UTF-8. – Alain V Sep 24 '16 at 21:40
  • 3
    @AlainV yes, you can force UTF-8 in Notepad. In the "Save as" dialog, there is an Encoding option. – Remy Lebeau Sep 24 '16 at 22:21
0

This Works very Well for me:

Your code looks perfectly fine if your .ini file is UTF-8 encoded and contains valid UTF-8 data. I don't know what is inside of your file, but it looks like your .ini file does not contain valid UTF-8 data.

So you'd have to find what encoding your .ini file uses. If your .ini file is ANSI, then you can open it like this:

SettingsFile := TIniFile.Create(aINIFileName, TEncoding.ANSI);