【问题标题】:How to go up a directory from current directory with GetPrivateProfileString()如何使用 GetPrivateProfileString() 从当前目录上移目录
【发布时间】:2019-06-26 06:36:12
【问题描述】:

我正在尝试使用 GetPrivateProfileString 从 ini 文件中读取我的程序的身份验证详细信息。我想上directory/Folder/File.ini) 但不知道该怎么做

我试过GetFullPathName()

void ini {


std::string iniPath = "/Ice/Ice.ini";
    LPWSTR inipath = A2W_EX(iniPath.c_str(), iniPath.length());



    DWORD IniPath = std::strtoul(iniPath.c_str(), NULL, 16);


    std::string playerUsername;

    std::string playerPassword;

    TCHAR iniauthChar[32];



    playerUsername = GetPrivateProfileString(authheader, authuser, 0, iniauthChar, 256, inipath);
    playerPassword = GetPrivateProfileString(authheader, authpass, 0, iniauthChar, 256, inipath);



}

这是我的ini文件,位于上面的目录

[AUTH]
Username=
Password=

【问题讨论】:

  • @TedLyngmo 包含文件系统并添加后namespace fs = std::filesystem; 文件系统不是std的成员
  • 您是否使用C++17 语言选项进行编译?如果您使用namespace fs = std::filesystem;,则可以使用fs::pathstd::filesystem::path 等。
  • 我不知道我在properties->C/C++->Language 下检查过,但没有看到C++ Language Standard 但是我可以告诉你我正在使用SDK Windows 8.1 和构建工具v140 这两个东西我无法改变。
  • 我对ini文件做了一些测试。我已经使用路径加载了它。它返回 NULL 作为结果

标签: c++ ini


【解决方案1】:

对于 Windows 上的目录路径,您应该使用 \\ 而不是 /

#include <windows.h>
#include <iostream>

int main()
{
    LPWSTR fn = L"Ice\\Ice.ini";

    wchar_t buf[MAX_PATH];      
    GetFullPathNameW(fn, MAX_PATH, buf, NULL);

    std::wcout << buf << std::endl;
}

或使用 ANSI 字符串:

#include <windows.h>
#include <iostream>

int main()
{
    LPSTR fn = "Ice\\Ice.ini";

    char buf[MAX_PATH];     
    GetFullPathNameA(fn, MAX_PATH, buf, NULL);

    std::cout << buf << std::endl;
}

【讨论】:

  • windows 中的大多数库函数都支持斜杠作为目录分隔符。
猜你喜欢
  • 2014-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-21
  • 2018-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多