How to get a unique filename?

How to get a unique filename?

Post by Fernando Piaz » Tue, 18 Feb 1997 04:00:00



 How can I get a unique new filename for a temporary file?

 I'm programming in C, and I want to create a temporary file in /tmp.
 Is there any function that gives me a new filename?

Thanks.

 
 
 

How to get a unique filename?

Post by Nemesi » Tue, 18 Feb 1997 04:00:00



>  How can I get a unique new filename for a temporary file?

>  I'm programming in C, and I want to create a temporary file in /tmp.
>  Is there any function that gives me a new filename?

> Thanks.

try mktemp(3C).

[ thanks Icarus, that's your answer :) ]
--
Alex Ramirez-Bellido
Computer Science FIB-UPC (Spain)

http://www.bath.ac.uk/~ma6arb/home.htm

"Daddy, what does <formatting drive c mean?>" ...

 
 
 

How to get a unique filename?

Post by Frederick Haa » Tue, 18 Feb 1997 04:00:00



>  How can I get a unique new filename for a temporary file?

[...in c...]

Try "tmpnam".

--
-=- Frederick Haab -=- Software Developer -=- Turner Production -=-

 
 
 

How to get a unique filename?

Post by Gerry S Haye » Tue, 18 Feb 1997 04:00:00




> >  How can I get a unique new filename for a temporary file?

> try mktemp(3C).

That's non-POSIX.  From the mktemp man page:

:CONFORMING TO
:       BSD 4.3. POSIX dictates tmpnam().

Try tmpnam() instead if you want portability.

Cordially,

  Sumner

--
Respond by post or email, but please don't CC: postings to me; my mailbox
is already quite full.

 
 
 

How to get a unique filename?

Post by Perry Pipla » Tue, 18 Feb 1997 04:00:00




>>  How can I get a unique new filename for a temporary file?

>>  I'm programming in C, and I want to create a temporary file in /tmp.
>>  Is there any function that gives me a new filename?

>> Thanks.
>Have a look at the man page for getpid:
>       getpid  returns  the  process  ID  of the current process.
>       (This is often used by routines that generate unique  tem-
>       porary file names.)

Better yet check out the man pages for tmpfile & tmpnam, they do it
automatically for you.

Time flies like arrows, but fruit flies like bananas.
Perry Piplani                http://perrypip.netservers.com


 
 
 

How to get a unique filename?

Post by Tom Mano » Tue, 18 Feb 1997 04:00:00



>  How can I get a unique new filename for a temporary file?

>  I'm programming in C, and I want to create a temporary file in /tmp.
>  Is there any function that gives me a new filename?

> Thanks.

I did "apropos temporary" and got some great suggestions.

Tom
--
Tom Manos
InfiNet - Norfolk, VA
**********************************************
Meddle not in the affairs of Wizards, for thou
art crunchy, and taste good with ketchup.

 
 
 

How to get a unique filename?

Post by Ian COWEL » Wed, 19 Feb 1997 04:00:00



>  How can I get a unique new filename for a temporary file?

>  I'm programming in C, and I want to create a temporary file in /tmp.
>  Is there any function that gives me a new filename?

> Thanks.

Have a look at the man page for getpid:
       getpid  returns  the  process  ID  of the current process.
       (This is often used by routines that generate unique  tem-
       porary file names.)

--
Ian Cowell

----------------------

 
 
 

How to get a unique filename?

Post by Geoff Sho » Wed, 19 Feb 1997 04:00:00


:  How can I get a unique new filename for a temporary file?
:
:  I'm programming in C, and I want to create a temporary file in /tmp.
:  Is there any function that gives me a new filename?

Use getpid() to find out the process number, and integrate this into
a file name.  Works as long as the process is running.

If you want the file to last longer than the process, you'll need to
keep a record of the last name to be used, and create a new filename
from this.  Or use the time.  In both cases you'll need some file locking
if more than one process is doing this at the same time.

        Geoff
--
----------------------------------------------------------------------------
Ever sit and watch ants? They're always busy with                Geoff Short

can't identify with that kind of work ethic. http://kipper.york.ac.uk/~geoff

 
 
 

How to get a unique filename?

Post by Amit Chatterje » Wed, 19 Feb 1997 04:00:00




> :  How can I get a unique new filename for a temporary file?
> :
> :  I'm programming in C, and I want to create a temporary file in /tmp.
> :  Is there any function that gives me a new filename?

> Use getpid() to find out the process number, and integrate this into
> a file name.  Works as long as the process is running.

> If you want the file to last longer than the process, you'll need to
> keep a record of the last name to be used, and create a new filename
> from this.  Or use the time.  In both cases you'll need some file locking
> if more than one process is doing this at the same time.

>         Geoff
> --

The getpid() method which you have mentioned above is not a good idea.
It will make it unnecessarily complicated and error-prone. Instead, use
the available functions mktemp(). It is compliant with SVID2 and XPG2
standards (maybe a part of ISO C standard also but I am not sure).

--
/***************************************************************************
Amit Chatterjee

All opinions are mine, not NORTEL's.
****************************************************************************/

 
 
 

How to get a unique filename?

Post by Gerry S Haye » Wed, 19 Feb 1997 04:00:00



> Instead, use the available functions mktemp(). It is compliant with
> SVID2 and XPG2 standards (maybe a part of ISO C standard also but I am
> not sure).

It's not a part of the ISO C standard.  The mktemp man page says:

:CONFORMING TO
:       BSD 4.3. POSIX dictates tmpnam().

so tmpnam() is a better choice for portability.

Cordially,

  Sumner

--
Respond by post or email, but please don't CC: postings to me; my mailbox
is already quite full.

 
 
 

How to get a unique filename?

Post by Andrae Mu » Fri, 28 Feb 1997 04:00:00




: >
:
: > Instead, use the available functions mktemp(). It is compliant with
: > SVID2 and XPG2 standards (maybe a part of ISO C standard also but I am
: > not sure).
:
: It's not a part of the ISO C standard.  The mktemp man page says:
:
: :CONFORMING TO
: :       BSD 4.3. POSIX dictates tmpnam().
:
: so tmpnam() is a better choice for portability.
:
According to the POSIX Programmers Guide (A great book by the way),

tmpnam () - Generates a string that is a valid non-existing file name.

#include <stdio.h>
char *tmpnam (char *s);

Generates a string that is a valid filename and is not the name of an
existing file.  The tmpnam() function generates a different name each time
it is called up to TMP_MAX times.

There is also a note that the BSD function mktemp() must be replaced by
tmpnam for POSIX compatability.

Do a man tmpnam, or buy the book for more info.

Andrae Muys.

 --
=========================================================================

                          |
Andrae Muys               | Linux... What do you want to DO today?
4th(and a half)Yr CSE     |
University of Queensland. | Diplomacy is the art of saying "Nice Doggie!"
Australia                 |    till you can find a rock. - Wynn Catlin

 
 
 

1. ensuring unique FTP upload filenames

I've got an interesting problem for the FTP server gurus out there...

We want an FTP site that allows tons of users to log into an anonymous
account and submit files.  The files must not overwrite each other, but
we don't want the users to have to worry about specifying a unique
filename.

Ideally, the FTP server would intercept the incoming file and change
its name to a unique identifier.

Is that possible somehow?

I appreciate any insights...

Sent via Deja.com http://www.deja.com/
Before you buy.

2. Routing Problem under Linux

3. unique filenames in Makefile

4. Announcing xfig 3.2.2 (already!)

5. automatically rename file to unique filename

6. Clock doesn't keep the correct time on p-133

7. Creation of unique filename for temp file.

8. Patch 108528-06 f**ks up fastpatch??

9. How to generate a unique filename

10. generate unique filename in ksh on AIX

11. Unique Filename

12. unique filenames?

13. What is the official way to create unique filenames in Unix?