Reputation: 1040
I am calling the method GetPrivateProfileStringA() in order to read values from a config.ini file, but when one of my values is over 3 digits, it cuts the value down to 3 (1234 would become 123).
I have a config.ini file which looks like this:
[PASSPORT]
MinWidth=200
MinHeight=300
[FEATURES]
MinEyeDistance=20
MaxEyeDistance=1000
...
I am reading this config.ini file using the following code:
char iniFilename[] = "C:\\config.ini";
char *iniVal = new char[256];
//check config.ini exists
if (std::ifstream(iniFilename))
{
GetPrivateProfileStringA("PASSPORT", "MinWidth", "200", iniVal, sizeof(iniVal), iniFilename);
configParam->minImageWidth = atol(iniVal);
GetPrivateProfileStringA("PASSPORT", "MinHeight", "300", iniVal, sizeof(iniVal), iniFilename);
configParam->minImageHeight = atol(iniVal);
GetPrivateProfileStringA("FEATURES", "MinEyeDistance", "10", iniVal, sizeof(iniVal), iniFilename);
configParam->minEyeDist = atol(iniVal);
GetPrivateProfileStringA("FEATURES", "MaxEyeDistance", "1000", iniVal, sizeof(iniVal), iniFilename);
configParam->maxEyeDist = atol(iniVal);
}
The buffer string itself (iniVal) gets 3 digits only, so I don't believe it has anything to do with converting the value to a long.
This is my configParam struct:
typedef struct _CONFIG_PARAMS
{
long minImageWidth;
long minImageHeight;
long minEyeDist;
long maxEyeDist;
} STRUCT_CONFIG_PARAMS;
I'm kind of lost as to why this is happening. Any help is appreciated!
Upvotes: 0
Views: 2504
Reputation: 191
You are using sizeof() for iniVal, which will return the size of pointer to char, not the size of the dynamically allocated array. Since this is 4 on your platform it leads to a truncation to 3 chars and a '\0'.
Either allocate iniVal on the stack using
char iniVal[256];
or use a constant to allocate the array and then pass this to GetPrivateProfileStringA() as in
const size_t ArraySize = 256;
char *iniVal = new char[ArraySize];
...
GetPrivateProfileStringA("PASSPORT", "MinWidth", "200", iniVal, ArraySize , iniFilename);
Upvotes: 2