I have this piece of code that refuses to return "DefaultVal" when "CurrentFile" is empty:
void __fastcall TForm1::Button2Click(TObject *Sender)
{
TIniFile *pIni = new TIniFile("c:\\Test\\MyIni.ini");
try
{
int i = pIni->ReadInteger (L"x", L"Level", 0); //This is ok
UnicodeString s = pIni->ReadString ("x", "CurrentFile", "DefaultVal"); //Debugger shows s = NULL
}
__finally
{
pIni->Free();
}
}
//---------------------------------------------------------------------------
This is the INI file:
[x]
CurrentFile=
If I edit the INI file to CurrentFile= "something"
then the code works and s correctly contains "something".
What am I doing wrong?
C++ Builder Tokyo 10.3.2
TIniFile::ReadString()
returns theDefault
value only if the specifiedIdent
value does not exist at all. If theIdent
value exists but is blank, or there is an error reading it, a blank string is returned instead. If you want yourDefault
value to be used if theIdent
value is blank, you will have to check for that manually, eg:Note that
TIniFile::ReadInteger()
returns theDefault
value if theIdent
value can't be converted to anint
for any reason, whether that be because it does not exist, it is blank, it cannot be read, it is not in numerical hexadecimal format, etc.