Name

tempnam, tmpfile, tmpnam
- temporary file routines

Library

libc.lib

Synopsis

  #include <stdio.h>
  FILE * tmpfile (void);
  char * tmpnam (char *str);
  char * tempnam (const char *tmpdir, const char *prefix);

Return values

The tmpfile function returns a pointer to an open file stream on success, and a NULL pointer on error.

The tmpnam and tempfile functions return a pointer to a file name on success, and a NULL pointer on error.


Detailed description

The tmpfile function returns a pointer to a stream associated with a file descriptor returned by the routine mkstemp. The created file is unlinked before tmpfile returns, causing the file to be automatically deleted when the last reference to it is closed. The file is opened with the access value w+’. The file is created in the directory determined by the environment variable TMPDIR if set. The default location if TMPDIR is not set is /tmp.

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.


Examples

#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:\system 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

         

Errors

The tmpfile function may fail and set the global variable errno for any of the errors specified for the library functions fdopen() or mkstemp().

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.


See also

mktemp

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top