2013-08-25 2 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; 

변경 wstring 유니 코드를 사용하지 않는 경우 GetTempPathAwchar_tcharGetTempPathW, string합니다.

-1

이 함수는 C 스타일 문자열을 사용하는 것 같습니다. 그러나 C++ String으로 변환 할 수 있습니다. 당신은 쉽게 찾을 경우

#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

관련 문제