Easy Tutorial
❮ C Examples Vowel Consonant C Examples Digits Count ❯

C Library Function - tmpfile()

C Standard Library - <stdio.h>

Description

The C library function FILE *tmpfile(void) creates a temporary file in binary update mode (wb+). The created temporary file is automatically deleted when the stream is closed or when the program terminates.

Declaration

Here is the declaration for the tmpfile() function.

FILE *tmpfile(void)

Parameters

Return Value

On success, the function returns a stream pointer to the created temporary file. If the file is not created, it returns NULL.

Example

The following example demonstrates the use of the tmpfile() function.

#include <stdio.h>

int main ()
{
   FILE *fp;

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

   /* You can use the temporary file here */

   fclose(fp);

   return(0);
}

Let's compile and run the above program, which will create a temporary file in the /tmp folder, but once the program exits, the temporary file will be automatically deleted, and the program will produce the following result:

Temporary file created

C Standard Library - <stdio.h>

❮ C Examples Vowel Consonant C Examples Digits Count ❯