This documentation is copyright © 1998-2001 Sandro Sigala <sandro@sigala.it>.
All rights reserved.
Released under the GNU General Public License.
tmpfile
Prototype
#include <stdio.h>
FILE *tmpfile(void);
Description
Creates a temporary binary file (with fopen
mode "wb+"
)
that will be automatically removed when it is closed or at
program termination. Returns a pointer to a stream
of the created file. If the file cannot be created, returns NULL
.
Example
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* tmpcheck.c: tryes to open a user specified number of temporary
* files and to write some data into the files.
* usage: tmpcheck <num>
*/
int main(int argc, char **argv)
{
FILE **tmpvec;
int i, num;
char data[] = "Some data";
if (argc != 2 || (num = atoi(argv[1])) < 1) {
fprintf(stderr, "usage: %s <num>\n", argv[0]);
return EXIT_FAILURE;
}
tmpvec = (FILE **)malloc(sizeof(FILE) * num);
if (tmpvec == NULL) {
fprintf(stderr, "%s: cannot allocate memory\n", argv[0]);
return EXIT_FAILURE;
}
for (i = 0; i < num; ++i) {
if ((tmpvec[i] = tmpfile()) == NULL) {
fprintf(stderr, "%s: %s\n", argv[0], strerror(errno));
return EXIT_FAILURE;
}
if (fwrite(data, 1, sizeof(data), tmpvec[i])
!= sizeof(data)) {
fprintf(stderr, "%s: %s\n", argv[0], strerror(errno));
return EXIT_FAILURE;
}
}
for (i = 0; i < num; ++i)
fclose(tmpvec[i]);
free(tmpvec);
printf("\nSuccess for %d temporary files\n", num);
return EXIT_SUCCESS;
}
References
ISO C 9899:1990 7.9.4.3