Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: In the mkstemp() example, do the unlink straight after the mkstemp()

...

Code Block
bgColor#ccccff
char sfn[] = "/tmp/temp-XXXXXX";
FILE *sfp;
int fd = -1;

if ((fd = mkstemp(sfn)) == -1) {
  /* Handle Error */
}

unlink(sfn); /* unlink immediately to allow name to be recycled */

if ((sfp = fdopen(fd, "w+")) == NULL) {
  unlink(sfn);
  close(fd);
  /* Handle Error */
}

unlink(sfn); /* unlink immediately to allow name to be recycled*/
/* use temporary file */

fclose(sfp); /* also closes fd */

...