tmpfileplus

tmpfileplus#

An all-singing, all-dancing C function to create a temporary file.

See https://www.di-mgt.com.au/c_function_to_create_temp_file.html for details.

Defines

TMPFILEPLUS_H_#
TMPFILE_KEEP#

Functions

FILE *tmpfileplus(const char *dir, const char *prefix, char **pathname, int keep)#

Create a unique temporary file.

Parameters:
  • dir – (optional) directory to create file. If NULL use default TMP directory.

  • prefix – (optional) prefix for file name. If NULL use “tmp.”.

  • pathname – (optional) pointer to a buffer to receive the temp filename. Allocated using malloc(); user to free. Ignored if NULL.

  • keep – If keep is nonzero and pathname is not NULL, then keep the file after closing. Otherwise file is automatically deleted when closed.

Throws:

ENOMEM – Not enough memory to allocate filename.

Returns:

Pointer to stream opened in binary read/write (w+b) mode, or a null pointer on error.

FILE *tmpfileplus_f(const char *dir, const char *prefix, char *pathnamebuf, size_t pathsize, int keep)#

Create a unique temporary file with filename stored in a fixed-length buffer.

Parameters:
  • dir – (optional) directory to create file. If NULL use default directory.

  • prefix – (optional) prefix for file name. If NULL use “tmp.”.

  • pathnamebuf – (optional) buffer to receive full pathname of temporary file. Ignored if NULL.

  • pathsize – Size of buffer to receive filename and its terminating null character.

  • keep – If keep is nonzero and pathname is not NULL, then keep the file after closing. Otherwise file is automatically deleted when closed.

Throws:

E2BIG – Resulting filename is too big for the buffer pathnamebuf.

Returns:

Pointer to stream opened in binary read/write (w+b) mode, or a null pointer on error.