![]() |
|
Create temp directory |
Steve Terry
Member #1,989
March 2002
![]() |
I need a way to create a temporary directory name. I've used tempnam(NULL) and it only seems to return 4 digit names with something like "\aer2." which is not exactly what I'm looking for. Anyone know of a better (no .NET) way of creating a unique temporary name consisting of 8-10 characters? ___________________________________ |
Rampage
Member #3,035
December 2002
![]() |
What I do when I need to create temp files is get the current time in milliseconds. That nomber is converted to a string and used as the name for my temp file/dir. -R |
bamccaig
Member #7,536
July 2006
![]() |
Yeah, date/time would have to be the only reliable way to generate a unique filename. Even then, because computers operate so fast it's possible for more than one to be created in the same milisecond (or whatever measure your method of getting time is)(depending on your application, of course) so you'll probably want a way to query the filesystem to make sure that the directory doesn't already exist. Besides, for all you know there was already a directory named 20070409103029.ext or the like from a separate application or user. Now that I think of it you might get away with hashing a random value, for example with SHA1 or MD5 or something, but I don't know how unique those are. Okay, generate a random value and hash that, salted with a numeric datetime! ...And then query the filesystem to make sure the file doesn't already exist (if it does start the process over). -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
tobing
Member #5,213
November 2004
![]() |
What's wrong with a name like "/aer2"? As long as the name refers to a directory that doesn't already exist... |
bamccaig
Member #7,536
July 2006
![]() |
tobing said: What's wrong with a name like "/aer2"? As long as the name refers to a directory that doesn't already exist... True, there's nothing wrong with that directory name. What was the problem with it? The only potential problem is the decreased probability of a 4-character string being unique; especially if the directory it's working in is used often or by numerous people at a time. The time is a better tactic anyway. In any case, you'll always have to check if the file exists or not before writing to make sure you don't damage another file. tempnam - Manual said: Creates a file with a unique filename, with access permission set to 0600, in the specified directory. If the directory does not exist, tempnam() may generate a file in the system's temporary directory, and return the name of that. In PHP it sounds like it actually returns a unique name, as in PHP checks that it doesn't exist yet (and might even create it for you). What language are you developing with?? -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
CGamesPlay
Member #2,559
July 2002
![]() |
Quote: tempnam(NULL) tempnam accepts two arguments: Quote:
SYNOPSIS char *tempnam(const char *dir, const char *pfx); DESCRIPTION Attempts to find an appropriate directory go through the following steps: a) In case the environment variable TMPDIR exists and contains the name b) Otherwise, if the dir argument is non-NULL and appropriate, it is c) Otherwise, P_tmpdir (as defined in <stdio.h>) is used when appropri- d) Finally an implementation-defined directory may be used. The string returned by tempnam() is allocated using malloc(3) and hence
[append] -- Ryan Patterson - <http://cgamesplay.com/> |
Steve Terry
Member #1,989
March 2002
![]() |
Ahh the documentation I was looking at only had tmpnam taking in one argument, not two. At any rate I just friggin used abs and rand to generate me a nice number (between 10000 and 99999) and used that for the directory name and checked if it existed first. ___________________________________ |
Thomas Fjellstrom
Member #476
June 2000
![]() |
I wrote a new tmpfile method for allegro 4.3 to handle all this. it atm, returns a file descriptor, so its just for files, but its not hard to make one for dirs either. It handles "anonymous" temp files (ones that are unlinked immediately), and can auto delete upon file close. I hadn't considered making a temp dir version. Maybe if people request it enough. code: (for the stdio handler, windows code will likely use one of the Win32 functions, if its deemed necessary)
It shouldn't be hard to make that do a dir at all. Or turn into code that doesn't rely on my Allegro 4.3 stuff. -- |
|