2013-08-25 53 views
2

好了,你都已經是一個巨大的幫助,今天和香港專業教育學院得到了最後一個問題,這將完成我的計劃,我希望不會是很難回答。獲取用戶臨時文件夾路徑C++

我想要做的就是抓住用戶的臨時文件夾路徑,並將其保存到的std :: string。

我能找到這個鏈接:http://msdn.microsoft.com/en-us/library/aa364992%28VS.85%29.aspx

與是我不明白如何拍攝,並將其保存爲一個字符串的鏈接的唯一問題。

回答

5
std::wstring strTempPath; 
wchar_t wchPath[MAX_PATH]; 
if (GetTempPathW(MAX_PATH, wchPath)) 
    strTempPath = wchPath; 

變化,如果你不使用Unicode wstringstringwchar_tcharGetTempPathWGetTempPathA

-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