Getting a string from .ini file for login not working. Please don't suggest one of those phases or anything. Unless this won't work. B
char* pResult = new char[255];
GetPrivateProfileString("login", "uname", "", pResult, 255, "C:\\Program Files\\myfile\\login.ini");
if (pResult == "1"){
g_pCVar->ConsoleColorPrintf(Color::Purple(),
"----Login-Succesfull----\n");
}
else{
g_pCVar->ConsoleColorPrintf(Color::Purple(),
"----Login-Failed----\n");
}
delete[] pResult;
Here is the .ini file.
[login]
uname=1
Could someone please suggest what the issue is. Could it be because I am reading from programs files. I had an issue when I was reading from temp? Thanks.
if (pResult == "1")
This is wrong, here you compare the pointers, not the actual data pointed by those. You should use something like if (std::strcmp(pResult,"1") == 0)
( strcmp
is case sensitive)
In windows
there's also a _stricmp
(case insensitive).
I remember back in the day I wrote a small helper like this one:
std::string get_profile_string(LPCSTR name, LPCSTR key, LPCSTR def, LPCSTR filename)
{
char temp[1024];
int result = GetPrivateProfileString(name, key, def, temp, sizeof(temp), filename);
return std::string(temp, result);
}
If small string optimization is enabled, it will be used if result
is small enough (so no memory allocation would happen). There's a limit of 1024 chars, which you can increase if you need of course.
The std::string
class overloads the equal to ==
operator so this time if (pResult == "1")
will actually work.
string result = get_profile_string("login", "uname", "", "C:\\Program Files\\myfile\\login.ini");
if (result == "1")
g_pCVar->ConsoleColorPrintf(Color::Purple(), "----Login-Succesfull----\n");
else
g_pCVar->ConsoleColorPrintf(Color::Purple(),"----Login-Failed----\n");
But ideally, if you simply want an integer, then you shouldn't use GetPrivateProfileString
at all. Instead, use GetPrivateProfileInt
int age = GetPrivateProfileInt("user", "age", 0, "C:\\Program Files\\myfile\\login.ini");
if (age >= 18)
{ }
else
{ }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.