[SOLVED] Re: length of file from NSFileHandle?
> On 27 Aug 2016, at 1:41 PM, Ken Thomaseswrote: > > The reason it has no length property is because not all NSFileHandles have > the concept of a length. For example, a file handle associated with a pipe > or socket. Right, though for those cases it could just return 0 or some other marker (or throw). > For an on-disk file, you can seekToEndOfFile and get offsetInFile to > determine the file length. If you need to not perturb the file position, you > can record the original offsetInFile and then seekToFileOffset:origOffset to > restore it after measuring the length. After I posted I noticed that -seekToEndOfFile returns the length, and I was calling that anyway (to append to the file), so it was an obvious solution I’d missed. > Matthew Emerson's suggestion of using fstat() with the fileDescriptor is also > reasonable. I was about to head down that path when I noticed the above - thanks for the suggestions anyway, worth knowing for the future. —Graham ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: length of file from NSFileHandle?
> On Aug 26, 2016, at 8:19 PM, Graham Coxwrote: > Apparently a simple task, but no obvious API for it: getting the length > (size) of a file I have a NSFileHandle for. This class has no -length > property, so how can I get it? > > I need to know because I have a requirement to create a backup copy of a file > once it exceeds a certain size. This backup is created when I first open the > file as a NSFileHandle. I can read the content of the file and find out the > size that way, but I’d rather not first read it into memory if I’m just going > to create my backup copy and then start over with an empty file - reading it > all in just to find the length seems a bit wrong, is all. Seems reasonable enough. You may need to use the Unix C api, fstat() defined in . See the man page for that. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: length of file from NSFileHandle?
On Aug 26, 2016, at 10:19 PM, Graham Coxwrote: > > Apparently a simple task, but no obvious API for it: getting the length > (size) of a file I have a NSFileHandle for. This class has no -length > property, so how can I get it? The reason it has no length property is because not all NSFileHandles have the concept of a length. For example, a file handle associated with a pipe or socket. For an on-disk file, you can seekToEndOfFile and get offsetInFile to determine the file length. If you need to not perturb the file position, you can record the original offsetInFile and then seekToFileOffset:origOffset to restore it after measuring the length. Matthew Emerson's suggestion of using fstat() with the fileDescriptor is also reasonable. Regards, Ken ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: length of file from NSFileHandle?
> On Aug 26, 2016, at 11:32 PM, R. Matthew Emerson> wrote: > > >> On Aug 26, 2016, at 11:19 PM, Graham Cox wrote: >> >> Hi all, >> >> Apparently a simple task, but no obvious API for it: getting the length >> (size) of a file I have a NSFileHandle for. This class has no -length >> property, so how can I get it? >> >> I need to know because I have a requirement to create a backup copy of a >> file once it exceeds a certain size. This backup is created when I first >> open the file as a NSFileHandle. I can read the content of the file and find >> out the size that way, but I’d rather not first read it into memory if I’m >> just going to create my backup copy and then start over with an empty file - >> reading it all in just to find the length seems a bit wrong, is all. > > One way would be to read the NSFileHandle's fileDescriptor property, and then > use stat(2), e.g., like so: > > #include > #include > > #include > > int main(int argc, char *argv[]) > { >struct stat buf; >stat("/etc/passwd", ); >printf("size = %lld\n", buf.st_size); > } Argh, I meant fstat(2). #include #include #include #include int main(int argc, char *argv[]) { struct stat buf; int fd = open("/etc/passwd", O_RDONLY); fstat(fd, ); printf("size = %lld\n", buf.st_size); } ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: length of file from NSFileHandle?
> On Aug 26, 2016, at 11:19 PM, Graham Coxwrote: > > Hi all, > > Apparently a simple task, but no obvious API for it: getting the length > (size) of a file I have a NSFileHandle for. This class has no -length > property, so how can I get it? > > I need to know because I have a requirement to create a backup copy of a file > once it exceeds a certain size. This backup is created when I first open the > file as a NSFileHandle. I can read the content of the file and find out the > size that way, but I’d rather not first read it into memory if I’m just going > to create my backup copy and then start over with an empty file - reading it > all in just to find the length seems a bit wrong, is all. One way would be to read the NSFileHandle's fileDescriptor property, and then use stat(2), e.g., like so: #include #include #include int main(int argc, char *argv[]) { struct stat buf; stat("/etc/passwd", ); printf("size = %lld\n", buf.st_size); } ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com