Borland C++ reference

tempnam

raw OCR

Creates a unique file name in specified directory.

Defined in header <stdio.h>

Syntax

#include <stdio.h>
char *tempnam(char   *dir, char *prefix)

Portability

DOSUNIXWindowsANSI CC++ only
■■■

Remarks

The tempnam function creates a unique filename in arbitrary directories. It attempts to use the following directories, in the order shown, when creating the file name:

B The directory specified by the TMP environment variable. H The dir argument to tempnam. Q The P_tmpdir definition in stdio.h. If you edit stdio.h and change this definition, tempnam will NOT use the new definition.

B The current working directory.

If any of these directories is NULL, or undefined, or does not exist, it is skipped.

The prefix argument specifies the first part of the filename; it cannot be longer than 5 characters, and may not contain a period (.). A unique filename is created by concatenating the directory name, the prefix, and 6 unique characters. Space for the resulting filename is allocated with malloc; the caller should free this filename when no longer needed by calling free. The unique file is not actually created; tempnam only verifies that it does not currently exist.

If you do create a temporary file using the name constructed by tempnam, it is your responsibility to delete the file name (for example, with a call to remove). It is not deleted automatically, (tmpfile does delete the file name.)

Return value

If tempnam is successful, it returns a pointer to the unique temporary file , name, which the caller may pass to free when it is no longer needed. Otherwise, if tempnam cannot create a unique filename, it returns NULL.

See also

Example

#include <stdio.h>
#include <stdlib.h>
void main (void)
{
   FILE *stream;
   int i;
   char *name;
   for  (i = 1; i <= 10; i++) {
      if  ((name = tempnamC'Wtmp" , "wow")) == NULL)
         perror ("tempnam couldn't create name");
      else  {
         printf ("Creating %s\n",name);
         if  ((stream = fopen(name,"wb")) == NULL)
            perror ("Could not open temporary file\n");
         else
            fclose (stream);

      free (name);
   }
   printf ("Warning: temp files not deleted.\n") ;

Differences from modern implementations

Nothing recorded yet.

Modern references

These are search links, not yet checked by hand.