Ch11.19: Temporary Files

Overview

Temporary files are used for storing data that only needs to exist for a short time. fast_io provides ::fast_io::io_temp for creating temporary files that are automatically deleted when closed.

Creating a Temporary File

To create a temporary file, pass ::fast_io::io_temp to any file type's constructor. The file is created in the system's temporary directory (e.g., /tmp on Unix, %TEMP% on Windows). When the file object goes out of scope, the file is automatically closed and deleted.


#include <fast_io.h>
#include <fast_io_device.h>

int main() {
    using namespace ::fast_io::iomnp;

    // Create a temporary file with obuf_file
    ::fast_io::obuf_file obf(::fast_io::io_temp);

    // Write to it like any other file
    println(obf, "Temporary data: ", 42);
    println(obf, "More data: ", 3.14159);

    // File is automatically deleted when `obf` goes out of scope
}

You can use ::fast_io::io_temp with any file type:


#include <fast_io.h>
#include <fast_io_device.h>

int main() {
    using namespace ::fast_io::iomnp;

    // Temporary file with C stdio interface
    ::fast_io::c_file cf(::fast_io::io_temp);
    fprintf(cf.fp, "Temporary data\n");

    // Temporary buffered file
    ::fast_io::obuf_file obf(::fast_io::io_temp);
    println(obf, "Buffered temporary data");

    // Temporary input/output file
    ::fast_io::iobuf_file iof(::fast_io::io_temp);
    println(iof, "Read-write temporary data");

    // All files are deleted automatically when they go out of scope
}

Important: The temporary file exists only as long as the file object is alive. Once it goes out of scope, the file is closed and deleted from the filesystem. This is RAII — you never forget to clean up temporary files.

Reading from a Temporary File

You can also read from a temporary file. Since temporary files are opened with both read and write access, you can write data, seek back to the beginning, and read it:


#include <fast_io.h>
#include <fast_io_device.h>

int main() {
    using namespace ::fast_io::iomnp;

    // Create a temporary file (read-write)
    ::fast_io::native_file temp(::fast_io::io_temp);

    // Write data
    println(temp, "Line 1");
    println(temp, "Line 2");
    println(temp, "Line 3");

    // Seek back to beginning
    ::fast_io::rewind(temp);

    // Read it back
    for (::fast_io::string line; scan<true>(temp, str_line_get(line)); ) {
        println("Read: ", line);
    }

    // File is deleted automatically when temp goes out of scope
}

Temporary Files vs Regular Files

Why use io_temp instead of just opening a regular file?

Compare this to the C standard library's tmpfile(), which also creates temporary files but returns a FILE* that you must manually close:


#include <stdio.h>

int main() {
    FILE *tmp = tmpfile();
    if (tmp == NULL) {
        perror("tmpfile failed");
        return 1;
    }
    fprintf(tmp, "Temporary data\n");
    fclose(tmp);  // File is deleted here
    return 0;
}

With fast_io's io_temp, you get RAII semantics — the file is automatically cleaned up, and you never forget to call fclose.

When to Use Temporary Files

Temporary files are useful for:

Key Takeaways