How to get temporary file name using tmpfile ()

Functions

tmpfile () says that:

The created temporary file is automatically deleted when the stream is closed (fclose) or during normal operation of the program. If the program is abnormally interrupted, whether the file is deleted depends on the particular system and library implementation. "

Function

tmpfile () returns a stream pointer to the generated temporary file, not the file path. I need a temporary path to the file name, since it needs to pass another library function to it.

My application may crash, so the tmpfile () function may work here if the output fails.

How can I get the temporary path to the file and delete the file automatically on exit

+4
source share
1

tmpfile() tmpname().

, .

. ( http://www.cplusplus.com/reference/cstdio/tmpnam/):

#include <stdio.h>

int main ()
{
   char buffer [L_tmpnam];
   char *pointer;

   tmpnam (buffer);
   printf ("Tempname #1: %s\n", buffer);

   pointer = tmpnam (NULL);
   printf ("Tempname #2: %s\n", pointer);

   return 0;  
}

.

Edit:

  • tmpnam . , fopen, .

  • . . , remove.

+1

All Articles