preface

I have been heartbroken for countless times when I have seen people create a file with a timestamp +arc4random(). Does the operating system not provide a function for this so I found the following code to solve the problem of creating the same file name.

/* Create a recording file */ NSString *filePath = [@"~/Movies/AVScreenShackRecording_XXXXXX" stringByStandardizingPath]; char *screenRecordingFileName = strdup([filePath fileSystemRepresentation]); if (screenRecordingFileName) { int fileDescriptor = mkstemp(screenRecordingFileName); if (fileDescriptor ! = -1) { NSString *filenameStr = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:screenRecordingFileName length:strlen(screenRecordingFileName)]; NSLog(@" unique filename :%@",filenameStr); } remove(screenRecordingFileName); free(screenRecordingFileName); }Copy the code

Before using

In the process of

After completion of

Note: It is best to use 6 X’s or more to refer to Linux

The main thing to understand is these two functions

Strdup () is a common string copy in C

The mkstemp() function creates and opens a file with a unique file name on the system

Citation: Original address