#include <stdio.h>
|
FILE *
tmpfile (void); |
char *
tmpnam (char *str); |
char *
tempnam (const char *tmpdir, const char *prefix); |
The tmpnam and tempfile functions return a pointer to a file name on success, and a NULL pointer on error.
The tmpnam function returns a pointer to a file name, in the P_tmpdir directory, which did not reference an existing file at some indeterminate point in the past. P_tmpdir is defined in the include file
#include <stdio.h .> If the argument
str
is
non- NULL,
the file name is copied to the buffer it references.
Otherwise, the file name is copied to a static buffer.
In either case,
tmpnam
returns a pointer to the file name.
The buffer referenced by str is expected to be at least L_tmpnam bytes in length. L_tmpnam is defined in the include file |
#include <stdio.h .>
The tempnam function is similar to tmpnam, but provides the ability to specify the directory which will contain the temporary file and the file name prefix. The environment variable TMPDIR (if set), the argument tmpdir (if non- NULL), the directory P_tmpdir, and the directory /tmp are tried, in the listed order, as directories in which to store the temporary file. The argument prefix, if non- NULL, is used to specify a file name prefix, which will be the first part of the created file name. The tempnam function allocates memory in which to store the file name; the returned pointer may be used as a subsequent argument to free. |
#include<stdio.h> //SEEK_SET, printf, tmpfile, FILE #include<sys/stat.h> //S_IWUSR int main() { //create the tmp directory mkdir("c:\\tmp", S_IWUSR); //call tmpfile to create a tempory file FILE* fp = tmpfile(); char buf[10]; if(fp) { //write onto the file fprintf(fp, "%s", "hello"); fflush(fp); //seek to the beginning of the file fseek(fp, SEEK_SET, 0); //beg of the file //read from the file fscanf(fp, "%s", buf); fflush(fp); //close the file fclose(fp); } printf("buf read: %s", buf); return 0; }
Output
buf read: hello
#include<stdio.h> //tmpnam, printf, FILE #include<sys/stat.h> //S_IWUSR #include<errno.h> //errno int main() { //create a directory c:ystem emp mkdir("c:\\system\\temp", S_IWUSR); char buf[L_tmpnam]; char rbuf[10]; //call tmpnam() to create a file char *rval = tmpnam(buf); errno = 0; //open the file with the name returned by tmpnam() FILE *fp = fopen(buf, "w"); if (fp == NULL) { printf("fopen of file returned by tmpnam() failed - errno %d ", errno); return -1; } if(fp) { fprintf(fp, "%s", "check"); fclose(fp); } fp = fopen(buf, "r"); if(fp) { fscanf(fp, "%s", rbuf); fclose(fp); } printf("read from file: %s\n", rbuf); printf("argument buf: %s\n", buf); printf("return value: %s\n", rval); return 0; }
Output
read from file: check argument buf: /System/temp/tmp.0.U9UPTx return value: /System/temp/tmp.0.U9UPTx
The tmpnam function may fail and set errno for any of the errors specified for the library function mktemp.
The tempnam function may fail and set errno for any of the errors specified for the library functions malloc or mktemp.
© 2005-2007 Nokia |