tmpfile() function in C

In C Programming Language, the tmpfile() function is used to produce/create a temporary file.

  • tmpfile() function is defined in the “stdio.h” header file.
  • The created temporary file will automatically be deleted after the termination of program.
  • It opens file in binary update mode i.e., wb+ mode.
  • The syntax of tmpfile() function is:
    FILE *tmpfile(void) 
  • The tmpfile() function always returns a pointer after the creation of file to the temporary file. If by chance temporary file can not be created, then the tmpfile() function returns NULL pointer.




// C program to demonstrate working of tmpfile()
#include <stdio.h>
int main()
    char str[] = "Hello GeeksforGeeks";
    int i = 0;
    FILE* tmp = tmpfile();
    if (tmp == NULL)
        puts("Unable to create temp file");
        return 0;
    puts("Temporary file is created\n");
    while (str[i] != '\0')
        fputc(str[i], tmp);
    // rewind() function sets the file pointer
    // at the beginning of the stream.
    while (!feof(tmp))



Temporary file is created
Hello GeeksforGeeks

This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :


Please write to us at to report any issue with the above content.