Domanda

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.

È stato utile?

Soluzione

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.

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top