c++ - GetPrivateProfileString 返回 "The system cannot find the file specified.",即使该文件存在

标签 c++ ini

在 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/

相关文章:

c++ - 将整数转换为字符串

C++ 在没有键名的情况下计算 INI 值

支持部分继承的 Python 配置解析器?

c++ - 通过传递不同的参数创建对象

c++ - std::function 类似委托(delegate)模板类

C++指针参数问题

c++ - 为什么 C++ 初始分配比 C 大得多?

Java 配置文件不特定于目录

java - Groovy/Java : Ini4j insert multiple values to single parameter in different lines

python - 如何将 INI 文件转换为 CSV 文件