tmpfile(3)
β¨ tmpfile(3) β the glow-up edition β¨
okay bestie, gather round, because today we're talking about a Library Functions Manual classic: tmpfile(3). she's small, she's humble, she makes temp files and then ghosts them. iconic behavior honestly. π
πΈ NAME πΈ
tmpfile β create a temporary file
no cap, that's the whole job description. she makes a temp file. that's it. that's the girl. π
π«Ά LIBRARY π«Ά
Standard C library (libc, -lc)
so you link with -lc like usual and you're set, you don't have to go hunting for anything exotic. slay.
β¨ SYNOPSIS β¨
#include <stdio.h>
FILE *tmpfile(void);
that's it. no arguments, no config, no drama. she takes zero parameters and she's not sorry about it. π
π¦ DESCRIPTION π¦
so here's the tea. tmpfile() opens a unique temporary file in binary read/write mode, which is w+b mode for those of you keeping notes. π
and then β the part that makes her main character energy β the file is automatically deleted</> when it gets closed, OR when the program terminates. like, she cleans up after herself. no leftover mess. no embarrassing temp files lying around six months later. we love a low-maintenance queen. π§
π₯ RETURN VALUE π₯
tmpfile() hands you back a stream descriptor (a FILE *, bestie, the good stuff). β¨
but if things go sideways β like, if a unique filename can't be generated, or if the unique file can't actually be opened β she returns NULL. π
and in that second case (the "couldn't open it" one, specifically), errno gets set to tell you what went wrong. so always check your return value, okay? don't be out here dereferencing NULL and then acting surprised. π
π ERRORS π
here's the whole roster of ways this can go wrong, and yes, we're listing all of them, because every single one matters:
EACCESβ search permission denied for a directory in the file's path prefix. basically: you're not allowed to look in there. π«EEXISTβ unable to generate a unique filename. she tried, she really did, but every name was taken. πEINTRβ the call was interrupted by a signal; go peek atsignal(7)for the full lore.EMFILEβ you hit the per-process limit on the number of open file descriptors. too many files open at once, babe. πENFILEβ you hit the system-wide limit on the total number of open files. this one's not even your fault, the whole machine is struggling. πENOSPCβ there was no room in the directory to add the new filename. the directory is full. no vacancy. π¨EROFSβ read-only filesystem. you cannot write here. she said no. π
π ATTRIBUTES π
for the full explanation of what these terms even mean, go read attributes(7). it's giving homework but it's giving useful homework. π
- Interface:
tmpfile()β Attribute: Thread safety β Value:MT-Safe
yes bestie, MT-Safe. she can hang out in multiple threads at once and not cause chaos. we stan a thread-safe function. π
β VERSIONS β
plot twist: the standard does not specify which directory tmpfile() will use. it's a mystery. she keeps her secrets. π€«
but glibc specifically will try the path prefix P_tmpdir, which is defined in <stdio.h>, and if that whole situation fails, she falls back to the directory /tmp. so that's where your file's probably living, iykyk. π
π STANDARDS π
C11, POSIX.1-2008.
certified, standardized, official. she's not a vibe she invented, she's a vibe that was ratified. π
π HISTORY π
POSIX.1-2001, C89, SVr4, 4.3BSD, SUSv2.
she's been around. she's seen things. an absolute elder of the C library and we respect her for it. πΈ
π NOTES π
here's a little fun fact with a legal disclaimer attached. POSIX.1-2001 specifies that an error message may be written to stdout if the stream cannot be opened.
β οΈ note the "may" bestie β that means it might happen, it's not guaranteed. so don't go building your whole error-handling strategy on a message showing up on stdout. check that return value instead. π«Ά
π¦ SEE ALSO π¦
go read these next, they're all in the same friend group:
and that's the whole page, bestie!! this was Linux man-pages 6.20, dated 2026-02-08, and we were talking about good old tmpfile(3) the entire time. πβ¨ go make some temp files, clean up after yourself, and remember: always check for NULL. mwah. ππ
π¦
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- Copyright 1993, David Metcalfe <david@prism.demon.co.uk>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms