CKTIS / C
tmpfile — creating a temporary file
tmpfile() creates a temporary binary file for reading and writing. It returns FILE * or NULL if file creation fails.
Version: C23; repository compiler flag, production runtime not attested
Syntax
#include <stdio.h>
FILE *tmpfile(void);tmpfile() from <stdio.h> takes no arguments. It creates a new file, distinct from every existing file, opened in "wb+" mode. It returns a stream, not a filename; check for NULL before use.
The file is automatically removed when closed or at program termination. Removal after abnormal termination is implementation-defined. Close the stream with fclose() and check the result.
The example writes text, then calls fseek(file, 0L, SEEK_SET) before reading. This resets the position and correctly separates writing from reading. Do not use the pointer after closing.
Examples
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *file = tmpfile();
if (file == NULL) {
return EXIT_FAILURE;
}
if (fputs("Hello\n", file) == EOF ||
fseek(file, 0L, SEEK_SET) != 0) {
fclose(file);
return EXIT_FAILURE;
}
char buffer[16];
if (fgets(buffer, sizeof buffer, file) == NULL) {
fclose(file);
return EXIT_FAILURE;
}
if (fclose(file) == EOF) {
return EXIT_FAILURE;
}
if (fputs(buffer, stdout) == EOF || fflush(stdout) == EOF) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Result
Hello
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.