从.ini文件中获取字符串

Jak*_*eid -2 c++ winapi

从.ini文件获取字符串以使登录无效.请不要建议其中一个阶段或任何事情.除非这不起作用.乙

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;
Run Code Online (Sandbox Code Playgroud)

这是.ini文件.

[login]
uname=1
Run Code Online (Sandbox Code Playgroud)

有人可以建议问题是什么.可能是因为我正在阅读程序文件.我从临时读书时遇到了一个问题?谢谢.

Jts*_*Jts 7

if (pResult == "1")
Run Code Online (Sandbox Code Playgroud)

这是错误的,在这里你比较指针,而不是那些指出的实际数据.你应该使用像if (std::strcmp(pResult,"1") == 0)(strcmp区分大小写)的东西

windows那里也有一个_stricmp(不区分大小写).

我记得在那天我写了一个像这样的小帮手:

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);
}
Run Code Online (Sandbox Code Playgroud)

如果启用了小字符串优化,则在result足够小时将使用它(因此不会发生内存分配).有1024个字符的限制,如果您需要,可以增加.

std::string类重载等于==运算符所以这个时候if (pResult == "1")会实际工作.

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");
Run Code Online (Sandbox Code Playgroud)

但理想情况下,如果您只是想要一个整数,那么您根本不应该使用它GetPrivateProfileString.相反,使用GetPrivateProfileInt

int age = GetPrivateProfileInt("user", "age", 0, "C:\\Program Files\\myfile\\login.ini");

if (age >= 18)
{ }    
else
{ }
Run Code Online (Sandbox Code Playgroud)