tmpfile() — Create Temporary File

Format

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

Language Level

ANSI

Threadsafe

Yes

Description

The tmpfile() function creates a temporary binary file. The file is automatically removed when it is closed or when the program is ended.

The tmpfile() function opens the temporary file in wb+ mode.

Return Value

The tmpfile() function returns a stream pointer, if successful. If it cannot open the file, it returns a NULL pointer. On normal end (exit()), these temporary files are removed.

On the Data Management system, the tmpfile() function creates a new file that is named QTEMP/QACXxxxxxx. If you specify the SYSIFCOPT(*IFSIO) option on the compilation command, the tmpfile() function creates a new file that is named /tmp/QACXaaaaaa. At the end of the job, the file that is created with the filename from the tmpfile() function is discarded. You can use the remove() function to remove files.

Example

This example creates a temporary file, and if successful, writes tmpstring to it. At program end, the file is removed.
#include <stdio.h>
 
FILE *stream;
char tmpstring[ ] = "This is the string to be temporarily written";
 
int main(void)
{
   if ((stream = tmpfile( )) == NULL)
      perror("Cannot make a temporary file");
   else
      fprintf(stream, "%s", tmpstring);
}

Related Information