gpt4 book ai didi

objective-c - cocoa NSFileManager 文件大小错误

转载 作者:行者123 更新时间:2023-12-03 17:13:38 25 4
gpt4 key购买 nike

我正在使用 NSFileManager 来获取应用程序中某些文件的大小。

每个文件都由单独的线程检查,所有文件的结果都是相同的错误值。以下代码在单独的线程中为每个文件运行:

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);

输出为:

Part 1: /Users/David/TEMP/245E0BF53ECA/part1 => size:36864 actual size:37187

Part 5: /Users/David/TEMP/245E0BF53ECA/part5 => size:36864 actual size:37187

Part 2: /Users/David/TEMP/245E0BF53ECA/part2 => size:36864 actual size:37187

Part 4: /Users/David/TEMP/245E0BF53ECA/part4 => size:36864 actual size:37187

Part 3: /Users/David/TEMP/245E0BF53ECA/part3 => size:36864 actual size:37187

Part 6: /Users/David/TEMP/245E0BF53ECA/part6 => size:36864 actual size:37187

Part 7: /Users/David/TEMP/245E0BF53ECA/part7 => size:36864 actual size:37186

Part 0: /Users/David/TEMP/245E0BF53ECA/part0 => size:36864 actual size:37189

此外,终端上 ls -l 命令的输出与之前保存在 myPart.Size 中的正确文件大小相同

-rw-r--r-- 1 David staff 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 part2

-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 staff 37187 Nov 25 11:25 part5

-rw-r--r-- 1 David staff 37187 Nov 25 11:25 part6

-rw-r--r-- 1 David staff 37186 Nov 25 11:25 part7

我使用的是 OS X 10.8 和 Xcode 4.5。

任何帮助将不胜感激。

最佳答案

我发现了实际问题!

文件在以下时间之前未关闭:

[[[fileManager attributesOfItemAtPath:fileName error:&error] objectForKey:NSFileSize] unsignedLongValue];

所以我关闭了与 pathfileName 变量关联的文件处理程序,问题就解决了。

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];

谢谢大家。

关于objective-c - cocoa NSFileManager 文件大小错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13549745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com