Question: My application generates loads of images and in order to save memory, I write these files to the temporary directory and read them when needed. I write two versions of the same image to the tmp folder one the thumbnail version at lower resolution and the other is full size. To make the file names unpredictable, I add a string hash at the end.

For instance I want to have two images one called "ThumbnailImage.fgl8bda" and the other "FullImage.fgl8bda"

This is the code I use:

```
NSString *fileName = @"Image.XXXXXXX";
NSString *thumbName = [@"Thumbnail" stringByAppendingFormat:@"%@", fileName];
NSString * thumbPath = [self writeToTempFile:thumbNailImage andName: thumbName];

NSString *fullName = [@"Full" stringByAppendingFormat:@"%@", fileName];
NSString *fullPath = [self writeToTempFile:fullImage andName: fullName];

```

However, the two files come out with different names, i.e. each time I use the fileName variable the hash is regenerated. For instance, my two images are called "ThumbnailImage.jhu078l" and "FullImage.ksi9ert".

Any idea how I can use the same hash more than once?

Answer: 
It is generally not safe to reuse a temporary file name suffix, because even if you can ensure `A.ashkjd` does not exist, there is a chance `B.ashkjd` is occupied.

You could construct a folder and store the two images in it, e.g.

```
char tmpl[] = "Image.XXXXXX";
char* res = mkdtemp(tmpl);
if (res != NULL) {
   NSString* dirName = [NSString stringWithUTF8String:res];
   NSString* thumbPath = [dirName stringByAppendingPathComponent:@"Thumbnail.png"];
   [thumbImage writeToFile:thumbPath atomically:YES];
   NSString* fullPath = [dirName stringByAppendingPathComponent:@"Full.png"];
   [fullImage writeToFile:fullPath atomically:YES];
}

```

(Note: you need to remove the folder manually.)
@TimG

It doesn't really make a difference. That too ends up in different names.

When I write

```
NSString *fileName = @"Image.XXXXXXX";

```

The `XXXXXXX` part of the fileName is replaced by 7 random characters so that the filenames are not predictable. 

Cheers