w3resource

C tmpfile() function

C library function - tmpfile()

The tmpfile() function is used to create a temporary file and open a corresponding stream. The file shall be automatically deleted when all references to the file are closed.

Note: The file shall be opened as in fopen() for update (wb+), except that implementations may restrict the permissions, either by clearing the file mode bits or setting them to the value S_IRUSR | S_IWUSR.

Syntax:

FILE *tmpfile(void);

Return value from tmpfile()

  • Upon successful completion, tmpfile() shall return a pointer to the stream of the file that is created.
  • Otherwise, it shall return a null pointer and set errno to indicate the error.

Example: tmpfile() function

The following example creates a temporary file for update, and returns a pointer to a stream for the created file in the fp variable.

#include <stdio.h>
int main () {
   FILE *fp;

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

   /* Here you can use tmp file. */

   fclose(fp);

   return(0);
}

Output:

Temporary file created..!

Errors: The value of error number can be set to:

Value Meaning
EINTR A signal was caught during tmpfile().
EMFILE All file descriptors available to the process are currently open.
ENFILE The maximum allowable number of files is currently open in the system.
ENOSPC The directory or file system which would contain the new file cannot be expanded.
EOVERFLOW The file is a regular file and the size of the file cannot be represented correctly in an object of type off_t.
ENOMEM Insufficient storage space is available.

C Programming Code Editor:

Previous C Programming: C setvbuf()
Next C Programming: C tmpnam()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/c-programming/stdio/c_library_method_tmpfile.php