gtpc2m7lC/C++ Language Support User's Guide

tmpfile-Create a Temporary File

This function creates a temporary binary file. It opens the temporary file in wb+ mode. The file is automatically removed when it is closed or when the program ends.

Format

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

Normal Return

If successful, this function returns a pointer to the stream associated with the file that is created.

Error Return

If the tmpfile function cannot open the file, it returns a NULL pointer.

The following are the possible values of errno:

EMFILE
OPEN_MAX file descriptors are currently open in the calling process.

FOPEN_MAX streams are currently open in the calling process.

ENFILE
The maximum number of files that are allowed is currently open in the system.

ENOSPC
The file system that will contain the new file cannot be expanded.

ENOMEM
There is not enough storage space available.

Programming Considerations

Temporary files cannot be created in 1052 or UTIL state.

Examples

The following example creates a temporary file and, if successful, writes the character string, tmpstring, to it. When the program ends, the file is removed.

#include <stdio.h>
 
int main(void) {
   FILE *stream;
   char tmpstring[] = "This string will be written.";
 
   if((stream = tmpfile( )) == NULL)
      printf("Cannot make a temporary file\n");
   else
      fprintf(stream, "%s", tmpstring);
}

Related Information

fopen-Open a File.

See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.