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?
- Automatic cleanup: The file is deleted when the object goes out of scope, even if an exception is thrown.
- Safe location: The file is created in the system's temporary directory, which is the appropriate place for temporary data.
- No naming conflicts: The system generates a unique name, so you don't need to worry about picking a filename.
- Security: On some systems, temporary files are created with restricted permissions.
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:
- Intermediate results: When processing large datasets, you might write intermediate results to a temporary file instead of keeping everything in memory.
- Caching: Cache computed results that are expensive to regenerate.
- Inter-process communication: Write data that another process will read (though pipes or shared memory are often better).
- Testing: Create temporary files for unit tests without polluting the filesystem.
Key Takeaways
-
::fast_io::io_tempis a tag passed to file constructors to create a temporary file in the system's temporary directory. -
The temporary file is automatically deleted when the file object goes out of scope.
On Linux, this uses
O_TMPFILE; on Windows,FILE_FLAG_DELETE_ON_CLOSE. -
Use
io_tempfor data that only needs to exist temporarily — you never forget to clean up. -
Temporary files are opened with both read and write access. Use
rewindorseekto move between reading and writing. -
Prefer
io_tempover manually managing temporary files with regular file APIs. -
Every file type accepts
io_temp—native_file,obuf_file,iobuf_file,c_file, etc.