Previous Page
Next Page

tmpfile

Opens a temporary file with a unique name

#include <stdio.h>
FILE *tmpfile( void );

The tmpfile( ) function opens a temporary file for reading and writing, and returns the corresponding FILE pointer. The file is guaranteed to have a distinct name and FILE pointer from all other files, and is automatically deleted when closed, whether by fclose( ) or by normal program termination. The file is opened with the mode string "wb+" (see fopen( ) in this chapter for a description of mode strings).

If tmpfile( ) is unable to open a temporary file, it returns a null pointer. Whether temporary files are deleted after an abnormal program termination depends on the given implementation. The C99 standard recommends that the maximum number of temporary files that can be created should be available in the macro TMP_MAX, which is defined in stdio.h and must be at least 25.

Example

FILE *fpTmp; *fpRx;
int c;

/* ... open Rx stream ... */

if (( fpTmp = tmpfile( ) ) == NULL )
  fputs( "Unable to open a temporary file.", stderr );
else
{
  while (( c = fgetc( fpRx )) != EOF )
    if ( fputc( c, fpTmp ) == EOF )
      break;
}
fclose( fpRx );

/* ... process the data captured in fpTmp ... */

See Also

fopen( ), tmpnam( )


Previous Page
Next Page