![](/img/trans.png)
[英]Qt: Issue getting the settings ini file from application/executable directory
[英]Getting a string from .ini file
从 .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;
这是 .ini 文件。
[login]
uname=1
有人可以建议问题是什么。 可能是因为我正在读取程序文件。 我从 temp 读取时遇到问题? 谢谢。
if (pResult == "1")
这是错误的,在这里您比较的是指针,而不是指针所指向的实际数据。 您应该使用类似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);
}
如果启用了小字符串优化,如果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");
但理想情况下,如果您只是想要一个整数,那么您根本不应该使用GetPrivateProfileString
。 相反,使用GetPrivateProfileInt
int age = GetPrivateProfileInt("user", "age", 0, "C:\\Program Files\\myfile\\login.ini");
if (age >= 18)
{ }
else
{ }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.