#include <netpbm/pm.h> FILE * pm_tmpfile(void);
This simple example creates a temporary file, writes "hello world" to it, then reads back and prints those contents.
#include <netpbm/pm.h> FILE * myfile; myfile = pm_tmpfile(); fprintf(myfile, "hello world\n"); fseek(myfile, 0, SEEK_SET); fread(buffer, sizeof(buffer), 1, myfile); fprintf(STDOUT, "temp file contains '%s'\n", buffer); fclose(myfile);
This library function is part of Netpbm.
pm_tmpfile() creates and opens an unnamed temporary file. It is basically the same thing as the standard C library tmpfile() function, except that it uses the TMPFILE environment variable to decide where to create the temporary file. If TMPFILE is not set or is set to something unusable (e.g. too long), pm_tmpfile() falls back to the value of the standard C library symbol P_tmpdir, just like tmpfile().
Unlike tmpfile(), pm_tmpfile() never returns NULL. If it fails, it issues a message to Standard Error and aborts the program, like most libnetpbm routines do.