This documentation is archived and is not being maintained.


Creates a temporary file. A version of tmpfile with security enhancements as described in Security Enhancements in the CRT.

errno_t tmpfile(
   FILE** pFilePtr


[out] pFilePtr

The address of a pointer to store the address of the generated pointer to a stream.

Returns 0 if successful, an error code on failure.

Error Conditions
pFilePtr Return Value Contents of pFilePtr



not changed

If the above parameter validation error occurs, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, errno is set to EINVAL and the return value is EINVAL.

The tmpfile function creates a temporary file and puts a pointer to that stream in the pFilePtr argument. The temporary file is created in the root directory. To create a temporary file in a directory other than the root, use tmpnam or tempnam in conjunction with fopen.

If the file cannot be opened, tmpfile_s writes NULL to the pFilePtr parameter. This temporary file is automatically deleted when the file is closed, when the program terminates normally, or when _rmtmp is called, assuming that the current working directory does not change. The temporary file is opened in w+b (binary read/write) mode.

Failure can occur if you attempt more than TMP_MAX_S (see STDIO.H) calls with tmpfile_s.

Routine Required header Compatibility



Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003

For additional compatibility information, see Compatibility in the Introduction.

// crt_tmpfile_s.c
// This program uses tmpfile to create a
// temporary file, then deletes this file with _rmtmp.

#include <stdio.h>

int main( void )
   FILE *stream;
   char tempstring[] = "String to be written";
   int  i;
   errno_t err;

   // Create temporary files.
   for( i = 1; i <= 3; i++ )
      err = tmpfile_s(&stream);
      if( err )
         perror( "Could not open new temporary file\n" );
         printf( "Temporary file %d was created\n", i );

   // Remove temporary files.
   printf( "%d temporary files deleted\n", _rmtmp() );


Temporary file 1 was created
Temporary file 2 was created
Temporary file 3 was created
3 temporary files deleted

Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.