What is the tmpfile function in C ?

mohamad wael
2 min readMay 8, 2021

--

The tempfile function has a signature of :

#include <stdio.h>FILE *tmpfile (void);

It is part of the stdio.h header , which is a header related to performing input and output operations , in a portable way , across different operating system . The stdio.h header is part of the C standard library .

The role of the tmpfile function , is to create temporary binary files , that can be used as one’s sees fit , for example to write or read temporary data .

You can think that it is as if this function , is calling the fopen function , which is part of stdio.h , and passing to it , a generated temp file name , and that the mode for this file , is set to wb+ . wb+ means , that the file is opened for reading and writing , and that it is a binary file .

The tmpfile function returns a pointer to a FILE structure, which is passed to other functions , such as fread , fwrite , fgetc , fputc … to read or write data .

If a temporary file cannot be created , the tmpfile function returns NULL .

The created temporary files , are automatically deleted , upon closing the file , for example by calling the fclose function , with the pointer to the FILE structure , passed to it . The created temporary files , are also automatically deleted , when the program is terminated .

It is implementation defined , if the temporary files created by the tempfile function , are deleted upon abnormal termination of the program .

An example of usage of the tmpfile function is as follows :

--

--

No responses yet