テンポラリのディレクトリの中のテンポラリのファイル名を取得するサンプル。
NSTemporaryDirectory()でテンポラリフォルダを取得。mkstempsでテンポラリファイル名が取得できるが、一旦書き込み可能な文字列の配列にコピーしないといけない。NSStringのlengthでは日本語とか含まれている場合、取得した長さが配列の長さと違うので、strlenで配列の長さを取得してコピーする。
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool =
[[NSAutoreleasePool alloc] init];
NSString *tempDir = NSTemporaryDirectory();
NSString *filePath =
[tempDir stringByAppendingPathComponent:
@"prefixXXXXXXsuffix"];
size_t bufferSize =
strlen([filePath fileSystemRepresentation]) + 1;
char buffer[bufferSize];
if ([filePath getFileSystemRepresentation:buffer
maxLength:bufferSize]) {
if (mkstemps(buffer, 6) != -1) {
NSLog(@"TemporaryFile = '%s'", buffer);
}
}
[pool drain];
return 0;
}