C Library Functions -tmpfile()

C Standard Library - <stdio.h>

Description

C Library FunctionsFILE *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

Below is the declaration of the tmpfile() function.

FILE *tmpfile(void)

Parameter

  • NA

Return Value

If successful, 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 usage of the tmpfile() function.

#include <stdio.h>

int main ()
{
   FILE *fp;

   fp = tmpfile();
   printf("临时文件被创建\n");

   /* 您可以在这里使用临时文件 */

   fclose(fp);

   return(0);
}

Let us compile and run the above program. It 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:

A temporary file is created.

C Standard Library - <stdio.h>

other extensions