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

</>
Copy
#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:

  1. FILE *temp = tmpfile(); calls the tmpfile() function to create a temporary file and assigns its file pointer to temp.
  2. The fprintf() function writes the string “Hello, temporary file!” to the temporary file.
  3. rewind(temp); resets the file pointer to the beginning of the file so that the contents can be read from the start.
  4. fgets(buffer, 50, temp); reads the contents of the file into the buffer.
  5. printf() outputs the content read from the temporary file.
  6. 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

</>
Copy
#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:

  1. char template[] = "/tmp/tempfileXXXXXX"; defines a template for the temporary filename. The XXXXXX are placeholders to be replaced by mkstemp().
  2. int fd = mkstemp(template); creates a unique temporary file based on the template and returns a file descriptor fd. The actual filename is updated in the template array.
  3. write(fd, data, strlen(data)); writes the string “Temporary file using mkstemp!” to the file using the file descriptor.
  4. lseek(fd, 0, SEEK_SET); resets the file pointer to the beginning of the file for reading.
  5. read(fd, buffer, sizeof(buffer) - 1); reads the file content into the buffer and ensures it is null-terminated.
  6. close(fd); closes the file descriptor, and unlink(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:

  1. tmpfile(): Provides an easy way to create a temporary file that is automatically deleted when closed.
  2. mkstemp(): Generates a unique temporary file and returns a file descriptor, giving more control over the file handling process.