How to generate a unique filename

How to generate a unique filename

Post by Dan Cro » Sat, 07 May 1994 01:18:55





Quote:>I'm looking for a C-function that will open a file with a unique
>filename. This may well be a RTFM but the FM is so big!

Check the functions tempnam() and tmpnam, these allow you to create
unique filenames.  Then just use those to open a file for temporary
access.  Also, there is a function tmpfile() which will return a
unique file descriptor.  (I think that this function is only availible
with ANSI C, though?)  Hope this helps some!

        - Dan C.

 
 
 

How to generate a unique filename

Post by Richard Soderber » Fri, 06 May 1994 22:25:27


I'm looking for a C-function that will open a file with a unique
filename. This may well be a RTFM but the FM is so big!
TIA
/RS

                                 (o o)
         +-------------------oOO--(_)--OOo----------------------+
         |    Richard Soderberg, MD, Systems analyst            |

         |    Voice#: +46 8 728 80 00  Fax#  : +46 8 33 04 81   |        

         +------------------------------------------------------+
         | 1. Things aren't what they seem to be.               |
         | 2. Things are far more complicated than you thought. |
         | 3. Regardless of 1 and 2 you have to try anyway.     |
         |                                      (myself)        |
         +------------------------------------------------------+

 
 
 

How to generate a unique filename

Post by Carl Brew » Tue, 10 May 1994 13:52:11






>>I'm looking for a C-function that will open a file with a unique
>>filename. This may well be a RTFM but the FM is so big!

>Check the functions tempnam() and tmpnam, these allow you to create
>unique filenames.  Then just use those to open a file for temporary
>access.  Also, there is a function tmpfile() which will return a
>unique file descriptor.  (I think that this function is only availible
>with ANSI C, though?)  Hope this helps some!

if you're using SunOS 4.x.x,  your best bet is to use mktemp(),
but FILE *tmpfile() and char *tmpnam() are ANSI, and _should_ work
on everything.

from TFM (SunOS 4.1.3)

mkstemp()  makes
the  same  replacement  to  the  template but returns a file
descriptor for the template file open for reading and  writ-
ing.   mkstemp() avoids the race between testing whether the
file exists and opening it for use.

So mkstemp() is the safest bet to avoid any race conditions ...

--
Carl Brewer                             Ph :61-9-380-1893 | #include \
Systems/Network Officer, Reid Library   Fax:61-9-380-1012 | <std_disclaimer.h>

Merlin, where are you?  Call your Dragon, to weave a mist ....        "BEABLE"