• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: length of file from NSFileHandle?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: length of file from NSFileHandle?


  • Subject: Re: length of file from NSFileHandle?
  • From: "R. Matthew Emerson" <email@hidden>
  • Date: Fri, 26 Aug 2016 23:32:37 -0400

> On Aug 26, 2016, at 11:19 PM, Graham Cox <email@hidden> 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 <sys/stat.h>
#include <stdlib.h>

#include <stdio.h>

int main(int argc, char *argv[])
{
    struct stat buf;
    stat("/etc/passwd", &buf);
    printf("size = %lld\n", buf.st_size);
}


_______________________________________________

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


  • Follow-Ups:
    • Re: length of file from NSFileHandle?
      • From: "R. Matthew Emerson" <email@hidden>
References: 
 >length of file from NSFileHandle? (From: Graham Cox <email@hidden>)

  • Prev by Date: length of file from NSFileHandle?
  • Next by Date: Re: length of file from NSFileHandle?
  • Previous by thread: length of file from NSFileHandle?
  • Next by thread: Re: length of file from NSFileHandle?
  • Index(es):
    • Date
    • Thread