C tmpfile() function

C library function - tmpfile()

The tmpfile() function is used to create a temporary file and open a corresponding stream. The file shall be automatically deleted when all references to the file are closed.

Note: The file shall be opened as in fopen() for update (wb+), except that implementations may restrict the permissions, either by clearing the file mode bits or setting them to the value S_IRUSR | S_IWUSR.


FILE *tmpfile(void);

Return value from tmpfile()

  • Upon successful completion, tmpfile() shall return a pointer to the stream of the file that is created.
  • Otherwise, it shall return a null pointer and set errno to indicate the error.

Example: tmpfile() function

The following example creates a temporary file for update, and returns a pointer to a stream for the created file in the fp variable.

#include <stdio.h>
int main () {
   FILE *fp;

   fp = tmpfile();
   printf("Temporary file created..!\n");

   /* Here you can use tmp file. */




Temporary file created..!

Errors: The value of error number can be set to:

Value Meaning
EINTR A signal was caught during tmpfile().
EMFILE All file descriptors available to the process are currently open.
ENFILE The maximum allowable number of files is currently open in the system.
ENOSPC The directory or file system which would contain the new file cannot be expanded.
EOVERFLOW The file is a regular file and the size of the file cannot be represented correctly in an object of type off_t.
ENOMEM Insufficient storage space is available.

C Programming Code Editor:

Previous C Programming: C setvbuf()
Next C Programming: C tmpnam()

Share this Tutorial / Exercise on : Facebook and Twitter

C Programming: Tips of the Day

What's an object file in C?

An object file is the real output from the compilation phase. It's mostly machine code, but has info that allows a linker to see what symbols are in it as well as symbols it requires in order to work. (For reference, "symbols" are basically names of global objects, functions, etc.)

A linker takes all these object files and combines them to form one executable (assuming that it can, i.e.: that there aren't any duplicate or undefined symbols). A lot of compilers will do this for you (read: they run the linker on their own) if you don't tell them to "just compile" using command-line options. (-c is a common "just compile; don't link" option.)

Ref : https://bit.ly/3CbzF8M