I am working on a piece of code in the old application, using C++ in Visual Studio 2008. It attempts to read some values from an INI file, and I'm trying to do some error handling in the case of the section or key I am trying to read does not exist in the file.
Here is the code snippet:
int nValue = GetPrivateProfileInt (SECTION, KEY, -1, sINIFile);
if(nValue == -1) // default value
{
int nLastError = GetLastError();
if(nLastError != 0)
{
// do something
}
}
sINIFile contains the full path to my required INI file.
After some testing, I've found that GetLastError() returns 2 (ERROR_FILE_NOT_FOUND) if:
the file doesn't exist OR,
the section doesn't exist OR,
the key doesn't exist.
I would like to know the specific reason the GetPrivateProfileInt returned the default value - is this possible?
Note: I've looked into checking the value of 'errno' (or errorno, as it says on the MSDN page) but this is always 0 for any of the above cases.