views:

1891

answers:

6

What is the best way of doing this? tmpnam() returns a path to a file in the root of the drive, which requires administrator privileges on Windows Vista, so this is not an option.

A: 

Have you tried with the environment variables TEMP and TMP set to a directory writable by all? To change environment variables in XP (not familiar with Vista), you go to System Properties, [Advanced] tab, [Environment Variables] button.

ΤΖΩΤΖΙΟΥ
+6  A: 

GetTempPath and GetTempFileName should work.

Timbo
Thanks, thats exactly what I was looking for.
Viktor
A: 

Use a GUID?

benefactual
+1  A: 

The environment variable %TEMP% on Windows points to the users temp directory.

In managed C++ you can call Path::GetTempFileName() which will give you a temporary file in the users temp directory (which can be found using Path::GetTempPath() ). GetTempFileName() basically just gives you a path to a file in the %TEMP% path using a GUID as the file name. You then use that path to create the file and do what you want with it. You could do similar logic in any language that has access to the current processes environment variables.

Hope that helps,

Martin.

Martin Woodward
A: 

Perhaps you could use the Win32 method GetTempPath() in kernel32.dll. This is wrapped in .NET by System.IO.Path.GetTempFileName().

On XP this returns a path in C:\Documents and Settings\username\Local Settings\Temp\, so you should not require admin privileges.

Matt Howells
A: 

If you care about interoperability, the man page for tmpnam suggests:

tmpnam man page

BUGS
       Never use this function. Use mkstemp(3) instead.

mkstemp man page

SYNOPSIS
       #include <stdlib.h>

       int mkstemp(char *template);

DESCRIPTION
       The mkstemp() function generates a unique temporary file name from template.  The last six characters of template must be
       XXXXXX and these are replaced with a string that makes the filename unique. The file is then created with mode read/write

but all these suggest that you have prepared your template prefixed by the contents of the TMP environment variable.

ΤΖΩΤΖΙΟΥ