在 CPP Visual Studio 2010 中。我需要从 ini 文件中读取一些配置。下面的代码不起作用。谁能帮我修一下。
char * path = "C:\\NotBackedUp\\Workspaces\\LDAP-DLL\\LDAPTestApp\\bin\\Debug\\conf\\ldap.ini";
std::wcout << "path: " << path << std::endl;
if(!ATLPath::FileExists(path))
{
HRESULT hr = ATL::AtlHresultFromLastError();
ATLTRACE("%x\n",hr);//system could not find the file specified!
std::cout << "File not found " << std::endl;
return 0;
}
else
{
std::cout << "File found " << std::endl;
}
char valueRead[320];
int a = GetPrivateProfileStringA("ldap", "url", "error", valueRead, 320, path);
std::cout << "Value Read " << valueRead << std::endl;
std::cout << "Error String " << GetLastErrorAsString();
上面的代码正在生成下面的日志,你可以看到 ATLPath::FileExists 返回 true,但是 getLastError 仍然表示系统找不到指定的文件
path: C:\NotBackedUp\Workspaces\LDAP-DLL\LDAPTestApp\bin\Debug\conf\ldap.ini
File found
Value Read error
Error String The system cannot find the file specified.
我的 ldap.ini 文件有以下几行并且在上面的路径中可用
[ldap]
url=ldap://testserver
非常感谢任何帮助
谢谢
最佳答案
您收到的 ERROR_FILE_NOT_FOUND 错误代码也是在 GetPrivateProfileString 无法找到部分或值时设置的。
使用您的 ini 文件,您的代码在我的 Win10 上运行良好。
使用十六进制查看器/编辑器验证您的 ldap.ini 实际上是 ASCII,并且它不包含 BOM .
关于c++ - GetPrivateProfileString 返回 "The system cannot find the file specified.",即使该文件存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44285057/