This documentation is archived and is not being maintained.


Creates a temporary file.

FILE *tmpfile( void );

Return Value

If successful, tmpfile returns a stream pointer. Otherwise, it returns a NULL pointer.


The tmpfile function creates a temporary file and returns a pointer to that stream. 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 returns a NULL pointer. 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 (see STDIO.H) calls with tmpfile.


Routine Required header Compatibility
tmpfile <stdio.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.


All versions of the C run-time libraries.


// crt_tmpfile.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;

   /* Create temporary files. */
   for( i = 1; i <= 3; i++ )
      if( (stream = tmpfile()) == NULL )
         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

See Also

Stream I/O Routines | _rmtmp | _tempnam | Run-Time Routines and .NET Framework Equivalents