Temporary Files

There are a number of variations on the theme of creating temporary files and directories. Here we’ll look at make-tmp-file and make-tmp-dir.

temporary-file.idio
;; use the default "idio-tmp-" filename root
fn := (libc/make-tmp-file)

printf "fn is %s\n" fn

fh := open-output-file fn

hprintf fh "Hello World\n"

close-handle fh

sb := libc/stat fn

printf "fn is %d bytes long\n" sb.st_size

libc/unlink fn
$ idio temporary-file
fn is /tmp/idio-tmp-9jQIws
fn is 12 bytes long

Alternatively, you can create a temporary directory:

temporary-dir.idio
dn := libc/make-tmp-dir "secret"

printf "dn is %s\n" dn

fn := append-string dn "/foo"
fh := open-output-file fn

hprintf fh "Hello World\n"

close-handle fh

sb := libc/stat fn

printf "%s is %d bytes long\n" fn sb.st_size

;; clean up everything
rm -rf dn
$ idio temporary-dir
dn is /tmp/secretIcDuyT
/tmp/secretIcDuyT/foo is 12 bytes long

Last built at 2024-12-21T07:11:31Z+0000 from 77077af (dev) for Idio 0.3