tmpfile : <stdio.h> library function

The function FILE *tmpfile(void); creates a temporary binary file in update mode. It created a temporary file with unique file name. Temporary file gets deleted automatically when the stream associated with it is closed using fclose or when program terminates normally.

Function prototype of tmpfile

FILE *tmpfile(void);
  • NONE

Return value of tmpfile

On success, this function returns a FILE pointer associated with the temporary file otherwise it returns NULL in case of failure.

C program to show the use of tmpfile function

The following program shows the use of tmpfile function to create a temporary file. This program performs standard input and output operations on temporary file like any other stream.

#include <stdio.h>

int main (){
   int c, counter = 0;
   FILE *file;
   
   /* Create a temporary file */
   file = tmpfile();
   fputs("tmpfile C Standard Library function", file);

   /* Rewind file pointer */
   rewind(file);
   
   while(!feof(file)){
       c = fgetc(file);
       printf("%c", c);
   }
   fclose(file);

   return(0);
}

Program Output
tmpfile C Standard Library function