Create a Temporary File for Intermediate Storage in C
A temporary file can be created for intermediate storage in C using built-in functions such as tmpfile()
or mkstemp()
. These functions allow you to create files that are automatically removed after use, ensuring that no residual data remains on disk.
Example 1: Creating a Temporary File using tmpfile()
In this example, we will create a temporary file using the tmpfile()
function. The file is automatically deleted when it is closed. We will write a string to the file and then read it back.
main.c
#include <stdio.h>
int main() {
// Create a temporary file; it will be deleted automatically when closed
FILE *temp = tmpfile();
if (temp == NULL) {
perror("Unable to create temporary file");
return 1;
}
// Write data to the temporary file
fprintf(temp, "Hello, temporary file!");
// Rewind the file pointer to the beginning for reading
rewind(temp);
char buffer[50];
fgets(buffer, 50, temp);
printf("Content: %s\n", buffer);
// Close the file; it is automatically deleted
fclose(temp);
return 0;
}
Explanation:
FILE *temp = tmpfile();
calls thetmpfile()
function to create a temporary file and assigns its file pointer totemp
.- The
fprintf()
function writes the string “Hello, temporary file!” to the temporary file. rewind(temp);
resets the file pointer to the beginning of the file so that the contents can be read from the start.fgets(buffer, 50, temp);
reads the contents of the file into the buffer.printf()
outputs the content read from the temporary file.- Finally,
fclose(temp);
closes the file, and since it is temporary, it is automatically deleted.
Output:
Content: Hello, temporary file!
Example 2: Creating a Temporary File using mkstemp()
In this example, we will create a temporary file using the mkstemp()
function. This function generates a unique filename from a template and returns a file descriptor. We will write data to the file, read it back, and then remove the file.
main.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main() {
// Define a template for the temporary filename
char template[] = "/tmp/tempfileXXXXXX";
// Create a unique temporary file and obtain its file descriptor
int fd = mkstemp(template);
if (fd == -1) {
perror("Unable to create temporary file");
return 1;
}
// Write data to the temporary file using the file descriptor
const char *data = "Temporary file using mkstemp!";
write(fd, data, strlen(data));
// Reset the file pointer to the beginning for reading
lseek(fd, 0, SEEK_SET);
char buffer[100];
read(fd, buffer, sizeof(buffer) - 1);
buffer[sizeof(buffer) - 1] = '\0';
printf("Content: %s\n", buffer);
// Close the file descriptor and remove the temporary file
close(fd);
unlink(template);
return 0;
}
Explanation:
char template[] = "/tmp/tempfileXXXXXX";
defines a template for the temporary filename. The XXXXXX are placeholders to be replaced bymkstemp()
.int fd = mkstemp(template);
creates a unique temporary file based on the template and returns a file descriptorfd
. The actual filename is updated in thetemplate
array.write(fd, data, strlen(data));
writes the string “Temporary file using mkstemp!” to the file using the file descriptor.lseek(fd, 0, SEEK_SET);
resets the file pointer to the beginning of the file for reading.read(fd, buffer, sizeof(buffer) - 1);
reads the file content into the buffer and ensures it is null-terminated.close(fd);
closes the file descriptor, andunlink(template);
deletes the temporary file from the filesystem.
Output:
Content: Temporary file using mkstemp!
Conclusion
In this tutorial, we learned two methods to create temporary files in C for intermediate storage:
tmpfile()
: Provides an easy way to create a temporary file that is automatically deleted when closed.mkstemp()
: Generates a unique temporary file and returns a file descriptor, giving more control over the file handling process.