C library function - tmpfile()
Advertisements
Description
The C library function FILE *tmpfile(void) creates a temporary file in binary update mode (wb+). The temporary file created is automatically deleted when the stream is closed (fclose) or when the program terminates.
Declaration
Following is the declaration for tmpfile() function.
FILE *tmpfile(void)
Parameters
NA
Return Value
If successful, the function returns a stream pointer to the temporary file created. If the file cannot be created, NULL is returned.
Example
The following example shows the usage of tmpfile() function.
#include <stdio.h>
int main ()
{
FILE *fp;
fp = tmpfile();
printf("Temporary file created\n");
/* you can use tmp file here */
fclose(fp);
return(0);
}
Let us compile and run the above program, it will create a temporay file in /tmp folder but once your program is out, it will be deleted automatically and program will produce the following result:
Temporary file created