Reputation: 1139
Alright you all have been a tremendous help today and ive got one last question which will finish my program and I am hoping wont be difficult to answer.
What I want to do is grab the users temp folder path and save it to an std::string.
I was able to find this link: http://msdn.microsoft.com/en-us/library/aa364992%28VS.85%29.aspx
The only issue with the link is I dont understand how to take that and save it to a string.
Upvotes: 4
Views: 8744
Reputation: 361
This function seems to use a C-Style String. However, you can convert it to a 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
You can also allocate and free memory using new[]
and delete[]
if you find it easier! You can use static memory allocation too!
I hope this helps... :D
Upvotes: -1
Reputation: 37192
std::wstring strTempPath;
wchar_t wchPath[MAX_PATH];
if (GetTempPathW(MAX_PATH, wchPath))
strTempPath = wchPath;
Change wstring
to string
, wchar_t
to char
and GetTempPathW
to GetTempPathA
if you're not using Unicode.
Upvotes: 7