tmpnam()

Creates a unique temporary file name.

  #include <stdio.h>
  
  char *tmpnam(char *s);    
Parameter

s

A temporary file name.

Remarks

The tmpnam() functions creates a valid filename character string that will not conflict with any existing filename. A program can call the function up to TMP_MAX times before exhausting the unique filenames that tmpnam() generates. The TMP_MAX macro is defined in stdio.h.

The s argument can either be a null pointer or pointer to a character array. The character array must be at least L_tmpnam characters long. The new temporary filename is placed in this array. The L_tmpnam macro is defined in stdio.h.

If s is NULL, tmpnam() returns with a pointer to an internal static object that can be modified by the calling program.

Unlike tmpfile(), a file created using a filename generated by the tmpnam() function is not automatically removed when it is closed. tmpnam() returns a pointer to a character array containing a unique, non-conflicting filename. If s is a null pointer ( NULL), the pointer refers to an internal static object. If s points to a character array, tmpnam() returns the same pointer.

This facility may not be available on configurations of the EWL that run on platforms without file systems.

Listing: Example of tmpnam() usage

#include <stdio.h>

#include <stdlib.h>

int main(void)

{

FILE *f;

char *tempname;

int c;

// get a unique filename

tempname = tmpnam("tempwerks");

// create a new file for output

if ( (f = fopen(tempname, "w")) == NULL) {

printf("Can't open temporary file %s.\n", tempname);

exit(1);

}

// output text to the file

fprintf(f, "shoe shirt tie trousers\n");

fprintf(f, "province\n");

// Close then delete the file.

fclose(f);

remove(tempname);

return 0;

}