Reputation: 520
I want to save file in temporary folder which can be easy found when type at run by putting "%temp%", but don't know how to navigate to them from c++.
I try using function like "GetTempPathA" or "GetTempFileNameA()" but they don't return char value. For my purpose I use "SaveToFile" method from "TResourceStream" and need UnicodeString, how to find those information?
Upvotes: 0
Views: 2376
Reputation: 126
One can use also getenv(), but it's deprecated and consider as "non-safe":
#pragma warning(disable:4996) //disable getenv warning
char* p = getenv("TEMP");
std::string path(p);
Upvotes: 0
Reputation: 244712
No, neither GetTempPath
nor GetTempFileName
return a char value. In general, C functions do not ever return strings. Instead, you pass in a string buffer and the length of that string buffer, and the function fills in the string buffer with the requested string.
For example, to call GetTempPath
, you would write the following code:
TCHAR szTempPathBuffer[MAX_PATH];
GetTempPath(MAX_PATH, // length of the buffer
szTempPathBuffer); // the buffer to fill
szTempPathBuffer
will contain the path to the temporary directory.
Do note that you should probably not be explicitly calling the ANSI functions (those with an A
suffix). Windows has been fully Unicode for over a decade now. Either use the macros defined by the Windows headers, which automatically resolve to the correct version of the function depending on whether _UNICODE
is defined, or explicitly call the Unicode versions (those with a W
suffix).
Since you're calling the ANSI versions, you're getting an ANSI string (composed of char
values), rather than a Unicode string (composed of wchar_t
values), which is presumably what the SaveToFile
method that you're trying to call expects.
Upvotes: 6