5.13. Temporary FilesThe ISO C standard defines two functions that are provided by the standard I/O library to assist in creating temporary files.
ExampleThe program in Figure 5.12 demonstrates these two functions.If we execute the program in Figure 5.12, we get$ ./a.out /tmp/fileC1Icwc /tmp/filemSkHSe one line of output Figure 5.12. Demonstrate tmpnam and tmpfile functions#include "apue.h" int main(void) { char name[L_tmpnam], line[MAXLINE]; FILE *fp; printf("%s\n", tmpnam(NULL)); /* first temp name */ tmpnam(name); /* second temp name */ printf("%s\n", name); if ((fp = tmpfile()) == NULL) /* create temp file */ err_sys("tmpfile error"); fputs("one line of output\n", fp); /* write to temp file */ rewind(fp); /* then read it back */ if (fgets(line, sizeof(line), fp) == NULL) err_sys("fgets error"); fputs(line, stdout); /* print the line we wrote */ exit(0); } Section 4.15 that unlinking a file does not delete its contents until the file is closed. This way, when the file is closed, either explicitly or on program termination, the contents of the file are deleted.The Single UNIX Specification defines two additional functions as XSI extensions for dealing with temporary files. The first of these is the tempnam function.[View full width]#include <stdio.h> char *tempnam(const char *directory , const char ![]() | ||||
Returns: pointer to unique pathname |
Example
The program in Figure 5.13 shows the use of tempnam.Note that if either command-line argumentthe directory or the prefixbegins with a blank, we pass a null pointer to the function. We can now show the various ways to use it:$ ./a.out /home/sar TEMP specify both directory and prefix
/home/sar/TEMPsf00zi
$ ./a.out " " PFX use default directory: P_tmpdir
/tmp/PFXfBw7Gi
$ TMPDIR=/var/tmp ./a.out /usr/tmp " " use environment variable; no prefix
/var/tmp/file8fVYNi environment variable overrides directory
$ TMPDIR=/no/such/dir ./a.out /home/sar/tmp QQQ
/home/sar/tmp/QQQ98s8Ui invalid environment directory is ignored
As the four steps that we listed earlier for specifying the directory name are tried in order, this function also checks whether the corresponding directory name makes sense. If the directory doesn't exist (the /no/such/dir example), that case is skipped, and the next choice for the directory name is tried. From this example, we can see that for this implementation, the P_tmpdir directory is /tmp. The technique that we used to set the environment variable, specifying TMPDIR= before the program name, is used by the Bourne shell, the Korn shell, and bash.
Figure 5.13. Demonstrate tempnam function
#include "apue.h"
int
main(int argc, char *argv[])
{
if (argc != 3)
err_quit("usage: a.out <directory> <prefix>");
printf("%s\n", tempnam(argv[1][0] != ' ' ? argv[1] : NULL,
argv[2][0] != ' ' ? argv[2] : NULL));
exit(0);
}
The second function that XSI defines is mkstemp. It is similar to tmpfile, but returns an open file descriptor for the temporary file instead of a file pointer.
#include <stdlib.h> int mkstemp(char *template ); |
Returns: file descriptor if OK, 1 on error |
