In standard C, there isn't a function exactly like tmpfile() that creates a temporary file and doesn't delete it on process termination. However, you can write your own function to create a non-deleting temporary file by using mkstemp() (POSIX) or a combination of tmpnam()/tempnam() with fopen().
Here's an example using mkstemp(), which is a safer alternative to tmpnam() as it directly opens the file, minimizing the window for a race condition:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
FILE *create_tempfile(char **filename) {
// Template for our file. XXXXXX will be replaced with a unique identifier.
char template[] = "/tmp/myprog.XXXXXX";
// Create a unique temporary file name and open it securely with mkstemp.
int fd = mkstemp(template);
// If mkstemp failed, return NULL.
if (fd == -1) {
perror("mkstemp");
return NULL;
}
// Optionally, store the filename
if (filename != NULL) {
*filename = strdup(template); // Caller should free the allocated filename
}
// Convert the file descriptor to a FILE pointer.
FILE *temp_file = fdopen(fd, "w+");
// If fdopen failed, close the file descriptor and return NULL.
if (!temp_file) {
close(fd);
perror("fdopen");
return NULL;
}
// The file by this point is open and will not be automatically deleted.
return temp_file;
}
int main() {
char *temp_filename = NULL;
FILE *temp_file = create_tempfile(&temp_filename);
if (!temp_file) {
return EXIT_FAILURE;
}
// Write data to the file as needed.
fprintf(temp_file, "This is a test!\n");
// Clean up.
fclose(temp_file); // Close the FILE stream.
if (temp_filename != NULL) {
printf("Temporary file created: %s\n", temp_filename);
free(temp_filename); // Free the allocated filename string.
}
// File is not deleted since it’s a regular file.
return EXIT_SUCCESS;
}
Remember to include the necessary headers to provide declarations for mkstemp(), fdopen(), and related functions.
This code works on POSIX-compliant systems (like Unix/Linux/macOS), where mkstemp() is available.
If you need a solution that is portable to non-POSIX environments, you can use the tmpnam() or tempnam() functions, but be aware of potential race conditions since there will be a gap between the generation of the filename and opening the file, where another process could potentially create a file with the same name:
#include <stdio.h>
#include <stdlib.h>
FILE *create_tempfile_portable() {
char temp_filename[L_tmpnam];
FILE *temp_file = NULL;
// Generate a temporary filename.
if (tmpnam(temp_filename)) {
// Open the file in read and write mode.
temp_file = fopen(temp_filename, "w+");
if (!temp_file) {
perror("fopen");
return NULL;
}
// Now you have a temporary file that will not be deleted automatically.
printf("Temporary file created: %s\n", temp_filename);
} else {
perror("tmpnam");
return NULL;
}
return temp_file;
}
Again, it's important to remember that tmpnam() and tempnam() are less secure than mkstemp(). They might not guarantee that the filename is unique by the time it is opened, so use mkstemp() where possible.