I am trying to find the file size. But my length is equl to zero

I am trying to find the file size.

 NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath]; 
//seek to begin of the file
[output seekToFileOffset:0];
NSData *mydata = [output availableData];
NSLog(@"length: %d", [mydata length]);

But my length is equl to zero. Why?

0
source share
2 answers

availableDatadesigned to read files. If you just want to know the file size, you actually do not need to open it. Just use NSFileManageras follows:

NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:self.finalPath error:NULL];
unsigned long long fileSize = [attributes fileSize]; // in bytes
+9
source

[fileHandler seekToEndOfFile], this returns the length of your file data, your code [output seekToFileOffset: 0] means 0 to 0, so there is no data

+2
source

All Articles