tags:

views:

4474

answers:

5

How do I find the 'temp' directory in Linux? I am writing a platform neutral C++ function that returns the temp directory. In Mac an Windows, there is an API that returns these results. In Linux, I'm stomped.

A: 

In standard c, you could try: P_tmpdir

jm
+5  A: 

Use the value of the $TMPDIR environment variable, and if that doesn't exist, use /tmp.

Greg Hewgill
+6  A: 

Be POSIX compliant, and use tmpnam (which will give you a full filename in a temporary location).

Adam Wright
+5  A: 

Check following variables:

  • The environment variable TMPDIR
  • The value of the P_tmpdir macro

If all fails try to use the directory `/tmp'.

You can also use tempnam function to generate a unique temporary file name.

Greg Dan
+1  A: 

Not so much an answer as another question: would the method that you use on OS X not work on linux? After all, OS X is another POSIX.

Jason Baker