Skip to content
Tutorials Center
My learning

Topics

C Reference ยท stdio.h

C tmpfile() function

Reference for C tmpfile(): create an anonymous binary temporary file that is deleted automatically, use it as scratch space, and see why tmpnam() is unsafe.

tmpfile() creates a new, uniquely named temporary file, opens it for reading and writing in binary mode ("wb+"), and returns the stream. You never see or choose the name. The file is deleted automatically when you close the stream or when the program ends normally.

It is the easy and safe way to get scratch space: sorting data that does not fit in memory, collecting output before you know whether it is good, or testing code that expects a FILE * without touching the real file system.

C
#include <stdio.h>

FILE *tmpfile(void);

Try it

C Write, rewind, read back

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Scratch space for a transformation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
FILE *tmpfile(void);

There are no parameters. The stream is opened as with fopen(name, "wb+"): read and write, binary, existing content discarded (there is none).

Returns: the new stream, or NULL if the file cannot be created (no writable temporary directory, too many open files, and so on), with errno set on most systems.

Errors / undefined behavior: none when the result is checked. Using the NULL result is undefined behavior.

Usage notes

  • Positioning between write and read. The stream is read-write, so you must call rewind(), fseek() or fflush() between writing and the following read, as the examples do.
  • Automatic deletion. The file is removed by fclose() and, if the program ends normally (return from main() or exit()), at exit. If the program is killed or crashes, an orphan file can stay behind; on POSIX systems implementations usually unlink the name immediately, so nothing is left.
  • Where it lives is up to the implementation (usually /tmp or the TMPDIR directory on Linux). The location is not available through the standard API.
  • You cannot rename or keep it. If you need a file that outlives the program or needs a known name, use fopen() with a name of your choice or POSIX mkstemp(), which returns a descriptor and fills in a unique name that you can remove or rename yourself.
  • Why not tmpnam()? It only returns a name. Between getting the name and creating the file, another program can create it first (a race), which is a classic security hole. tmpnam() is deprecated in POSIX; tmpfile() has no such gap because name and file are created together.
  • Size. It is a real file on disk, so it can be much larger than memory. It may also be slower than a memory buffer. For small data, fmemopen() keeps everything in RAM.
  • Always check for NULL. Sandboxed or read-only environments may refuse to create temporary files.