2013-04-24 80 views
4

如何獲取臨時文件夾並設置臨時文件路徑?我嘗試了代碼波紋管,但它有錯誤。非常感謝你!如何獲取臨時文件夾並設置臨時文件路徑?

TCHAR temp_folder [255]; 
GetTempPath(255, temp_folder); 

LPSTR temp_file = temp_folder + "temp1.txt"; 
//Error: IntelliSense: expression must have integral or unscoped enum type 

回答

3

此代碼添加了兩個指針。

LPSTR temp_file = temp_folder + "temp1.txt"; 

concatenating字符串,它不是創造你想要的結果字符串的任何存儲。

對於C風格的字符串,使用lstrcpylstrcat

TCHAR temp_file[255+9];     // Storage for the new string 
lstrcpy(temp_file, temp_folder);  // Copies temp_folder 
lstrcat(temp_file, T("temp1.txt")); // Concatenates "temp1.txt" to the end 

基於the documentation for GetTempPath,這也將是明智的MAX_PATH+1取代的255所有出現在你的代碼。

+0

關於使用MAX_PATH + 1的好處 – Muscles 2013-04-24 03:53:02

1

您不能將兩個字符數組一起添加並獲得有意義的結果。它們是指針,而不是像std :: string這樣的提供這種有用操作的類。

創建一個足夠大的TCHAR數組並使用GetTempPath,然後使用strcat爲其添加文件名。

TCHAR temp_file [265]; 
GetTempPath(255, temp_file); 
strcat(temp_file, "temp1.txt"); 

理想情況下,您還應該測試GetTempPath的失敗結果。就我從另一個答案中鏈接的文檔中可以看出,失敗的最可能原因是提供的路徑變量太小。按照推薦的那樣使用MAX_PATH + 1 + 9。