Creating a temporary file

C++:
Creating a temporary file

How to:

Here’s how to create and use a temporary file in current C++:

#include <cstdio>
#include <filesystem>
#include <iostream>

int main() {
    // Create a unique temporary file using the filesystem library
    std::filesystem::path temp_path = std::filesystem::temp_directory_path() /= std::tmpnam(nullptr);

    // Open the temporary file
    std::FILE* temp_file = std::fopen(temp_path.c_str(), "w+");
    if (!temp_file) {
        std::perror("File opening failed");
        return EXIT_FAILURE;
    }

    // Write something to it
    std::fputs("Hello, Temp World!\n", temp_file);

    // Always remember to close the file
    std::fclose(temp_file);

    // Output the path to our temporary file
    std::cout << "Temporary file created at: " << temp_path << std::endl;

    // Cleanup: delete the temporary file
    std::filesystem::remove(temp_path);

    return EXIT_SUCCESS;
}

Sample output (actual path will vary):

Temporary file created at: /tmp/abc123

Deep Dive

Temporary files come in handy in cases like saving state, sorting large datasets, or handling output that doesn’t need to persist. Historically, temp files were created in a common directory (like /tmp on Unix systems) with a simple naming scheme, risking collisions. Modern C++ uses the <filesystem> library to avoid such issues.

Alternatives include using RAM-based temporary storage (like tmpfs in most Unix-like systems) or database blobs. These methods keep the ephemeral data in memory or managed systems, reducing I/O overhead and improving performance.

Implementation wise, remember that:

  • File I/O can fail, so always check your file operations for errors.
  • Always close your files to prevent resource leaks.
  • Clean up: Delete your temporaries (although the system often does, it’s a good habit).

See Also