Temporary Files and Directories in C

Our program demonstrates how to work with temporary files and directories. This is useful for creating data that isn’t needed after the program exits, preventing pollution of the file system over time.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

void check(int result) {
    if (result == -1) {
        perror("Error");
        exit(1);
    }
}

int main() {
    // The easiest way to create a temporary file is by using tmpfile().
    // It creates a file and opens it for reading and writing.
    FILE *f = tmpfile();
    check(f == NULL ? -1 : 0);

    // Display the name of the temporary file. On Unix-based OSes,
    // the actual filename is not accessible, so we'll skip this part.
    printf("Temp file created (name not accessible)\n");

    // We can write some data to the file.
    fwrite((char[]){1, 2, 3, 4}, sizeof(char), 4, f);

    // If we intend to create many temporary files, we may prefer to create
    // a temporary directory. We can use mkdtemp() for this purpose.
    char template[] = "/tmp/sampledir-XXXXXX";
    char *dname = mkdtemp(template);
    check(dname == NULL ? -1 : 0);
    printf("Temp dir name: %s\n", dname);

    // Now we can synthesize temporary file names by
    // prefixing them with our temporary directory.
    char fname[256];
    snprintf(fname, sizeof(fname), "%s/file1", dname);
    FILE *file = fopen(fname, "w");
    check(file == NULL ? -1 : 0);
    fwrite((char[]){1, 2}, sizeof(char), 2, file);
    fclose(file);

    // Clean up
    fclose(f);
    // In a real program, you'd want to recursively delete the temp directory
    // and its contents. For simplicity, we'll just remove the file we created.
    remove(fname);
    rmdir(dname);

    return 0;
}

To compile and run the program:

$ gcc temp_files_and_dirs.c -o temp_files_and_dirs
$ ./temp_files_and_dirs
Temp file created (name not accessible)
Temp dir name: /tmp/sampledir-Ab3X5c

This C program demonstrates the creation and use of temporary files and directories. Here are some key points:

  1. We use tmpfile() to create a temporary file. Unlike in some other languages, C doesn’t provide a way to get the name of this file.

  2. For creating a temporary directory, we use mkdtemp(). This function creates a directory with a unique name based on the template we provide.

  3. We can then create files within this temporary directory by constructing file paths that start with the temporary directory’s path.

  4. It’s important to clean up temporary files and directories when they’re no longer needed. In this example, we remove the file we created and the temporary directory.

  5. Error checking is crucial in C. We use a check function to verify the success of operations and exit the program if an error occurs.

Remember that the exact behavior might vary slightly depending on the operating system. This code is designed for UNIX-like systems and may need modifications for other platforms.