Equivalent for Java's System.getProperty("java.io.tmpdir") in C?
As the title says, the java.io.tmpdir
property in Java is really nice, and I can't find an equivalent for C. I'm writing for Windows and in Visua开发者_高级运维l Studio. I don't want to just use something like C:\Temp
because in later versions of windows the appropriate directory could be something like C:\Users\[UserName]\AppData\Local\Temp
and a way to retrieve this would be much easier. I want a directory and not just a temp file because I'm downloading and executing a file from the internet.
As a side note, if anyone can give me tips about how to better google for C stuff it would be much appreciated :)
If you're using the Win32 API, you can use GetTempPath().
It is not quite what you were asking, but in the C standard library:
- tmpnam_r will create a filename string
- tmpfile will create and open a file (returning a FILE*)
See: http://www.gnu.org/s/libc/manual/html_node/Temporary-Files.html for more possibilities
For the directory itself, you can either get the dirname of the filename generated by the above functions, or in Windows you can get the environmental variable TEMP, and on Unix-likes you can either get the variable TMPDIR or use /tmp if TMPDIR is not set
精彩评论