GetTempFileName

2.x

  int GetTempFileName(bDriveLetter, lpszPrefixString, uUnique, lpszTempFileName)    
  BYTE bDriveLetter; /* suggested drive, */  
  LPCSTR lpszPrefixString; /* address of filename prefix */
  UINT uUnique; /* number to use as prefix */
  LPSTR lpszTempFileName; /* address of buffer for created filename */

The GetTempFileName function creates a temporary filename of the following form:

drive:\path\prefixuuuu.TMP

The following list describes the filename syntax:

Element Description

drive Drive letter specified by the bDriveLetter parameter
path Path of the temporary file (either the Windows directory or the directory specified in the TEMP environment variable)
prefix All the letters (up to the first three) of the string pointed to by the lpszPrefixString parameter
uuuu Hexadecimal value of the number specified by the uUnique parameter

Parameters

bDriveLetter

Specifies the suggested drive for the temporary filename. If this parameter is zero, Windows uses the current default drive.

lpszPrefixString

Points to a null-terminated string to be used as the temporary filename prefix. This string must consist of characters in the OEM-defined character set.

uUnique

Specifies an unsigned short integer. If this parameter is nonzero, it will be appended to the temporary filename. If the parameter is zero, Windows uses the current system time to create a number to append to the filename.

lpszTempFileName

Points to the buffer that will receive the temporary filename. This string consists of characters in the OEM-defined character set. This buffer should be at least 144 bytes in length to allow sufficient room for the path.

Return Value

The return value specifies a unique numeric value used in the temporary filename. If the uUnique parameter is nonzero, the return value specifies this same number.

Comments

Temporary files created with this function are not automatically deleted when Windows shuts down.

To avoid problems resulting from converting an OEM character string to a Windows string, an application should call the _lopen function to create the temporary file.

The GetTempFileName function uses the suggested drive letter for creating the temporary filename, except in the following cases:

If a hard disk is present, GetTempFileName always uses the drive letter of the first hard disk.

If, however, a TEMP environment variable is defined and its value begins with a drive letter, that drive letter is used.

If the TF_FORCEDRIVE bit of the bDriveLetter parameter is set, the preceding exceptions do not apply. The temporary filename will always be created in the current directory of the drive specified by bDriveLetter, regardless of the presence of a hard disk or the TEMP environment variable.

If the uUnique parameter is zero, GetTempFileName attempts to form a unique number based on the current system time. If a file with the resulting filename exists, the number is increased by one and the test for existence is repeated. This continues until a unique filename is found; GetTempFileName then creates a file by that name and closes it. No attempt is made to create and open the file when uUnique is nonzero.

Example

The following example uses the GetTempFileName function to create a unique temporary filename on the first available hard disk:

HFILE hfTempFile;
char szBuf[144];



/* Create a temporary file. */

GetTempFileName(0, "tst", 0, szBuf);

hfTempFile = _lcreat(szBuf, 0);

if (hfTempFile == HFILE_ERROR) {
    ErrorHandler();
}

See Also

GetTempDrive, _lopen