问题
I am trying to read a .init config file in c++ having the content.
[Ipaddress]
Ipaddress=169.254.115.22
[ScanConfiguration]
Scanfrequency=2500
ScanResolution=2500
StartAngle=700000
StopAngle=1100000
Till Now, I have used this code for reading the data. My project is of Unicode character set and hence used L before the string values.
int iScanFreq =GetPrivateProfileInt(L"ScanConfiguration",L"Scanfrequency", 2500, L"filename.ini");
int iScanRes =GetPrivateProfileInt(L"ScanConfiguration",L"ScanResolution", 2500, L"filename.ini");
int iStartAngle =GetPrivateProfileInt(L"ScanConfiguration",L"StartAngle", -450000, L"filename.ini");
int iStopAngle =GetPrivateProfileInt(L"ScanConfiguration",L"StopAngle", 2250000, L"filename.ini");
But I am getting only the default values in the variables not the correct values from the file. I have not done anything with the registry yet. Is there anything I have to do in registry for getting the correct value..
Any suggestions will be helpful Thanks.
回答1:
One idea comes to mind: GetPrivateProfileString and friends have a bit of a quirk with how they find the INI file. Unless you specify a path to the INI file (even something as simple as .\filename.ini
), they assume the file is located in the Windows directory. This is almost certainly not what you want, and will probably lead to not finding the file, and thus to default values.
Also, don't expect full Unicode support from those functions. They're just Unicode wrappers around ANSI text.
来源:https://stackoverflow.com/questions/10239576/getprivateprofileint-reading-only-the-default-values