[英]cocoa NSFileManager wrong file size
I'm using NSFileManager to get the size of some files in our application. 我正在使用NSFileManager来获取应用程序中某些文件的大小。
Each file is examined by a separate thread and the result for all the files is the same wrong value. 每个文件都由单独的线程检查,并且所有文件的结果都是相同的错误值。 The following code is ran for each file in a separate thread:
在单独的线程中为每个文件运行以下代码:
NSFileManager *fileManager= [[NSFileManager alloc] init];
NSUInteger completedFileSize = [[[fileManager attributesOfItemAtPath:fileName error:&error] objectForKey:NSFileSize] unsignedLongValue];
NSLog(@"Part %lu: %@ => size:%lu actual size:%lu", myPart.SequenceNumber, fileName, completedFileSize, myPart.Size);
The output is: 输出为:
Part 1: /Users/David/TEMP/245E0BF53ECA/part1 => size:36864 actual size:37187
第1部分:/ Users / David / TEMP / 245E0BF53ECA / part1 =>尺寸:36864实际尺寸:37187
Part 5: /Users/David/TEMP/245E0BF53ECA/part5 => size:36864 actual size:37187
第5部分:/ Users / David / TEMP / 245E0BF53ECA / part5 => size:36864实际大小:37187
Part 2: /Users/David/TEMP/245E0BF53ECA/part2 => size:36864 actual size:37187
第2部分:/ Users / David / TEMP / 245E0BF53ECA / part2 =>尺寸:36864实际尺寸:37187
Part 4: /Users/David/TEMP/245E0BF53ECA/part4 => size:36864 actual size:37187
第4部分:/ Users / David / TEMP / 245E0BF53ECA / part4 => size:36864实际大小:37187
Part 3: /Users/David/TEMP/245E0BF53ECA/part3 => size:36864 actual size:37187
第3部分:/ Users / David / TEMP / 245E0BF53ECA / part3 =>尺寸:36864实际尺寸:37187
Part 6: /Users/David/TEMP/245E0BF53ECA/part6 => size:36864 actual size:37187
第6部分:/ Users / David / TEMP / 245E0BF53ECA / part6 => size:36864实际大小:37187
Part 7: /Users/David/TEMP/245E0BF53ECA/part7 => size:36864 actual size:37186
第7部分:/ Users / David / TEMP / 245E0BF53ECA / part7 => size:36864实际大小:37186
Part 0: /Users/David/TEMP/245E0BF53ECA/part0 => size:36864 actual size:37189
第0部分:/ Users / David / TEMP / 245E0BF53ECA / part0 =>尺寸:36864实际尺寸:37189
Also output of ls -l command on terminal is identical to the correct file sizes previously saved in myPart.Size 终端上ls -l命令的输出也与先前保存在myPart.Size中的正确文件大小相同。
-rw-r--r-- 1 David staff 37189 Nov 25 11:25 part0
-rw-r--r-- 1 David员工37189 Nov 25 11:25 part0
-rw-r--r-- 1 David staff 37187 Nov 25 11:25 part1
-rw-r--r-- 1 David staff 37187 Nov 25 11:25 part1
-rw-r--r-- 1 David staff 37187 Nov 25 11:25 part2
-rw-r--r-- 1 David staff 37187 Nov 25 11:25 part2
-rw-r--r-- 1 David staff 37187 Nov 25 11:25 part3
-rw-r--r-- 1 David staff 37187 Nov 25 11:25 part3
-rw-r--r-- 1 David staff 37187 Nov 25 11:25 part4
-rw-r--r-- 1 David员工37187 Nov 25 11:25 part4
-rw-r--r-- 1 David staff 37187 Nov 25 11:25 part5
-rw-r--r-- 1 David员工37187 Nov 25 11:25 part5
-rw-r--r-- 1 David staff 37187 Nov 25 11:25 part6
-rw-r--r-- 1 David staff 37187 Nov 25 11:25 part6
-rw-r--r-- 1 David staff 37186 Nov 25 11:25 part7
-rw-r--r-- 1 David员工37186 Nov 25 11:25 part7
I'm using OS X 10.8 and Xcode 4.5. 我正在使用OS X 10.8和Xcode 4.5。
Any help would be highly appreciated. 任何帮助将不胜感激。
I Found the actual problem! 我发现了实际的问题!
The file is not closed before the: 在以下情况下文件未关闭:
[[[fileManager attributesOfItemAtPath:fileName error:&error] objectForKey:NSFileSize] unsignedLongValue];
so I closed the file handler associated with fileName variable for path and the problem is solved. 所以我关闭了与fileName变量关联的文件处理程序的路径 ,问题得以解决。
fclose(myFile); //this is the handler for fileName
//now the value returned from the below line is correct.
[[[fileManager attributesOfItemAtPath:fileName error:&error] objectForKey:NSFileSize] unsignedLongValue];
Thanks from everyone. 谢谢大家
我认为您应该使用defaultManager和fileSize来计算文件大小。
unsigned long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:someFilePath error:nil] fileSize];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.