Creating Temporary Files

This chapter will examine a number of ways that a temporary file can be created under UNIX. Each of these has its advantages and disadvantages. The tmpnam(3) function is discouraged and is covered only because you will encounter it in existing code. The remaining functions can be used in new software.

Using the tmpnam(3) Function

The tmpnam(3) function generates a pathname for a new temporary file but does not create the temporary file itself. Its function synopsis is as follows:

#include <stdio.h>

char *tmpnam(char *buf);  /* Discouraged */

This function generates a temporary pathname in the directory given by the macro name P_tmpdir (defined in <stdio.h>). The argument buf must be null or point to a character buffer ...

Get Advanced UNIX Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.