tmpfile

tmpfile(): resource|false

Creates an anonymous w+b temp file handle that is auto-deleted on fclose or script end - made for burn-after-use buffering and swap scenarios.

C C23

C23
#include <stdio.h>

int main(void) {
    // C stdio tmpfile(): w+b mode, auto-removed on fclose()
    FILE *h = tmpfile();
    size_t w = fwrite("scratch", 1, 7, h);
    rewind(h); // same as fseek(h, 0, SEEK_SET)
    char buf[8] = {0};
    size_t n = fread(buf, 1, 7, h);
    buf[n] = '\0';
    printf("written: %zu\n", w);
    printf("read back: %s\n", buf);
    fclose(h); // the temp file is removed at this moment
    printf("cleanup: yes\n");
    return 0;
}
Output
written: 7
read back: scratch
cleanup: yes

C++ C++26

C++26
#include <cstdio>

int main() {
    // C++ has no RAII temp file in the standard library; std::tmpfile from <cstdio> is the usual pick
    FILE *h = std::tmpfile();
    std::fwrite("scratch", 1, 7, h);
    std::rewind(h);
    char buf[8] = {0};
    std::size_t n = std::fread(buf, 1, 7, h);
    buf[n] = '\0';
    std::printf("written: 7\n");
    std::printf("read back: %s\n", buf);
    std::fclose(h); // auto-delete happens here
    std::printf("cleanup: yes\n");
}
Output
written: 7
read back: scratch
cleanup: yes

C# v11.0.100

v11.0.100
// No automatic temp file: GetTempFileName creates it, you delete it manually
using System.IO;

string path = Path.GetTempFileName();
File.WriteAllText(path, "scratch");
Console.WriteLine("written: 7");
Console.WriteLine("read back: " + File.ReadAllText(path));
File.Delete(path); // manual - there is no auto-delete
Console.WriteLine("cleanup: yes");
Output
written: 7
read back: scratch
cleanup: yes

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"os"
)

func main() {
	// os.CreateTemp makes the file in the OS temp dir; deletion is your job - defer os.Remove
	f, err := os.CreateTemp("", "scratch-")
	if err != nil {
		panic(err)
	}
	defer os.Remove(f.Name()) // the auto-delete: deferred cleanup
	n, _ := f.WriteString("scratch")
	f.Seek(0, 0)
	buf := make([]byte, 8)
	m, _ := f.Read(buf)
	f.Close()
	fmt.Printf("written: %d\n", n)
	fmt.Printf("read back: %s\n", buf[:m])
	fmt.Println("cleanup: yes")
}
Output
written: 7
read back: scratch
cleanup: yes

Java v27.0.1

v27.0.1
import java.io.File;
import java.io.FileWriter;
import java.nio.file.Files;

public class Main {
    public static void main(String[] args) throws Exception {
        // File.createTempFile + deleteOnExit: JVM-hook auto delete
        File f = File.createTempFile("scratch-", ".tmp");
        f.deleteOnExit();
        FileWriter w = new FileWriter(f);
        w.write("scratch");
        w.close();
        String content = Files.readString(f.toPath());
        System.out.println("written: 7");
        System.out.println("read back: " + content);
        System.out.println("cleanup: yes");
    }
}
Output
written: 7
read back: scratch
cleanup: yes

JavaScript v26.13.1

v26.13.1
// Node has no tmpfile(); roll your own: unique name in os.tmpdir() + manual rm
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const p = path.join(os.tmpdir(), `scratch-${process.pid}.txt`);
fs.writeFileSync(p, "scratch");
console.log("written: 7");
console.log("read back:", fs.readFileSync(p, "utf8"));
fs.rmSync(p); // manual cleanup
console.log("cleanup: yes");
Output
written: 7
read back: scratch
cleanup: yes

PHP v8.6.0

v8.6.0
// tmpfile(): a w+b handle whose file vanishes on fclose() or script end
$h = tmpfile();
$w = fwrite($h, "scratch");
fseek($h, 0);
echo "written: $w\n";
echo "read back: ", fread($h, 7), "\n";
fclose($h); // the temp file is removed at this moment
echo "cleanup: yes\n";
Output
written: 7
read back: scratch
cleanup: yes

Python v3.15.2

v3.15.2
# tempfile.NamedTemporaryFile(delete=True): the with-block close deletes it
import tempfile

with tempfile.NamedTemporaryFile(mode="w+", delete=True) as h:
    written = h.write("scratch")
    h.seek(0)
    print(f"written: {written}")
    print(f"read back: {h.read()}")
print("cleanup: yes")
Output
written: 7
read back: scratch
cleanup: yes

Rust v1.99.1

v1.99.1
use std::fs::{self, OpenOptions};
use std::io::{Read, Seek, SeekFrom, Write};

fn main() {
    // Rust std has no temp file: hand-roll a unique path in std::env::temp_dir()
    let path = std::env::temp_dir().join(format!("scratch-{}.tmp", std::process::id()));
    let mut h = OpenOptions::new().read(true).write(true).create(true).open(&path).unwrap();
    let written = h.write(b"scratch").unwrap();
    h.seek(SeekFrom::Start(0)).unwrap();
    let mut s = String::new();
    h.read_to_string(&mut s).unwrap();
    println!("written: {}", written);
    println!("read back: {}", s);
    drop(h);
    fs::remove_file(&path).unwrap(); // manual cleanup
    println!("cleanup: yes");
}
Output
written: 7
read back: scratch
cleanup: yes

TypeScript v26.13.1

v26.13.1
// Node has no tmpfile(); roll your own: unique name in os.tmpdir() + manual rm
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
const p: string = path.join(os.tmpdir(), `scratch-${process.pid}.txt`);
fs.writeFileSync(p, "scratch");
console.log("written: 7");
console.log("read back:", fs.readFileSync(p, "utf8"));
fs.rmSync(p); // manual cleanup
console.log("cleanup: yes");
Output
written: 7
read back: scratch
cleanup: yes

Language differences

C: tmpfile() is the standard answer - w+b and auto-removed; you cannot even see its path (stream_get_meta_data is a PHP-only backdoor).

C++: no RAII temp file in the standard library - use std::tmpfile from <cstdio>; third-party RAII wrappers exist.

C#: Path.GetTempFileName creates but NEVER auto-deletes - File.Delete is mandatory; Path.GetRandomFileName avoids creating a file at all.

Go: os.CreateTemp + defer os.Remove is the canonical pair.

Java: File.createTempFile + deleteOnExit registers a JVM-exit hook.

JavaScript: nothing built in - os.tmpdir() plus a pid-based unique name, rmSync in a finally block.

PHP: tmpfile() is the carefree auto-deleting option; tempnam() creates without deleting (pair it with unlink); sys_get_temp_dir() gives the directory.

Python: the tempfile module covers everything - NamedTemporaryFile(delete=True) deletes when the with block exits.

Rust: no temp file in std; the third-party tempfile crate is the de-facto standard; hand-rolled means pid-based unique names.

TypeScript: same as JavaScript.