Re: Creating a series of sequenced files
Re: Creating a series of sequenced files
- Subject: Re: Creating a series of sequenced files
- From: Quincey Morris <email@hidden>
- Date: Sat, 16 Apr 2016 14:58:17 -0700
- Feedback-id: 167118m:167118agrif8a:167118sQeEngraHv:SMTPCORP
On Apr 16, 2016, at 14:36 , Carl Hoefs <email@hidden> wrote:
>
> I have a daemon process that needs to generate a series of sequenced files (named sequentially, such as "file_01944576_1.dat", "file_01944576_2.dat", etc.) in the same directory. Does Cocoa provide a way to do this?
Not that I know of. But surely it’s only 2 lines of code, apart from error checking and recovery?
> NSFileManager's -createFileAtPath:contents:attributes: method states:
> "If a file already exists at path, this method overwrites the contents of that file…"
Do not use this method: it’s obsolete. Use NSData.writeToURL:options: instead, with the NSDataWritingWithoutOverwriting option.
> I would hate to do this blindly, such as with fstat() in a loop, because there will potentially be many sequences, and each can grow to an arbitrary number. How do Finder and other OS X agents accomplish this?
Blindly in what sense? Even you have a lot of files, those are the files you want. There’s nothing inherently dangerous in creating them. If you mean, because of the possibility of conflicts with existing files, you should follow the normal best practices inside your ‘writeToFile’ loop:
— (Optional) Preflight the loop to ensure that conflicting files don’t exist. This is only an approximate safety measure, since conflicting files can be created after you check, but provides a better user experience if there is some reasonable expectation of conflict.
— Detect any actual conflict by the failure of ‘writeToFile’ and offer user recovery options at that point.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden