tmpfile() — Create temporary file
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
ISO C |
both |
Format
#include <stdio.h>
FILE *tmpfile(void);
General description
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 is terminated.
Note: When the tmpfile() function is issued from
multiple tasks within one address space, the temporary file names
may not be unique. The execution of the tmpfile() function concurrently
within one address space will result in errors. For example, an open
will fail because the file is already open.
Returned value
If successful, tmpfile() returns a pointer to the stream associated with the file created.
If tmpfile() cannot open the file, it returns a NULL pointer. On normal termination (exit()), these temporary files are removed. On abnormal termination, an effort is made to remove these files.
Returned value for POSIX C
When the calling application is a z/OS® UNIX program, the temporary file is created in the hierarchical file system. The file is created in the directory referred to by the TMPDIR environment variable, or '/tmp' if TMPDIR is not defined.
Special behavior
for XPG4:The
following are the possible values of errno:
- Error Code
- Description
- EINTR
- A signal was caught during tmpfile().
- 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 allowable number of files is currently open in the system.
- ENOMEM
- Insufficient storage space is available.
- ENOSPC
- The directory or file system which would contain the new file cannot be expanded.
Example
CELEBT13
/* CELEBT13
This example creates a temporary file and if successful,
writes tmpstring to it.
At program termination, 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);
}
}