【问题标题】:Get the users temp folder path c++获取用户临时文件夹路径 c++
【发布时间】:2013-08-27 19:47:52
【问题描述】:

好吧,今天你们都帮了大忙,我有最后一个问题将完成我的程序,我希望不会很难回答。

我想要做的是获取用户临时文件夹路径并将其保存到 std::string。

我找到了这个链接:http://msdn.microsoft.com/en-us/library/aa364992%28VS.85%29.aspx

链接的唯一问题是我不明白如何将其保存到字符串中。

【问题讨论】:

    标签: c++ string directory temp


    【解决方案1】:

    这个函数似乎使用了 C 风格的字符串。但是,您可以将其转换为 C++ 字符串。

    #define MAX_LENGTH 256 // a custom maximum length, 255 characters seems enough
    
    #include <cstdlib> // for malloc and free (optional)
    #include <string>
    
    using namespace std;
    
    // other code
    
    char *buffer = malloc(MAX_LENGTH);
    string temp_dir;
    
    if (GetTempPath(MAX_LENGTH, buffer) != 0) temp_dir = string(buffer);
    else {/* GetTempPath returns 0 on error */}
    
    free(buffer); // always free memory used for the C-Style String
    
    // other code
    

    如果您觉得更简单,您还可以使用new[]delete[] 分配和释放内存!你也可以使用静态内存分配!

    我希望这会有所帮助... :D

    【讨论】:

      【解决方案2】:
      std::wstring strTempPath;
      wchar_t wchPath[MAX_PATH];
      if (GetTempPathW(MAX_PATH, wchPath))
          strTempPath = wchPath;
      

      如果您不使用 Unicode,请将 wstring 更改为 stringwchar_t 更改为 charGetTempPathW 更改为 GetTempPathA

      【讨论】:

        猜你喜欢
        • 2010-11-22
        • 2013-10-08
        • 2013-07-26
        • 2013-07-05
        • 2012-08-19
        • 1970-01-01
        • 2016-11-10
        • 2019-12-24
        • 2018-01-21
        相关资源
        最近更新 更多