0
NSString *pathOfFile = [NSString stringWithFormat:@"%@", file];
NSFileHandle *fh = [NSFileHandle fileHandleForUpdatingAtPath:pathOfFile];
[fh seekToFileOffset:30];
NSData *data = [fh readDataToEndOfFile];
[fh writeData:nil];
[[NSFileManager defaultManager] createFileAtPath:file contents:data attributes:nil];

我想剪切文件的前 30 个字节。我得到的是:

mbp:Debug me$ hexdump -C File1
00000000  08 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000030  00 00 00 00 00 00 00 00  00 00 00 00 06 00 00 00  |................|
00000040  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000070  00 00 00 00 00 00 00 00  03 00 00 00 00 00 00 00  |................|
00000080  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000000b0  00 00 00 00 02 00 00 00  00 00 00 00 00 00 00 00  |................|
000000c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000000f0  01 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000100  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000120
mbp:Debug me$ hexdump -C File1
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 06 00  |................|
00000020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000050  00 00 00 00 00 00 00 00  00 00 03 00 00 00 00 00  |................|
00000060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000090  00 00 00 00 00 00 02 00  00 00 00 00 00 00 00 00  |................|
000000a0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000000d0  00 00 01 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000000e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000100

为什么它会因被削减而归零?

4

1 回答 1

0

总而言之,我想出了这个:

    NSString *pathOfFile = [NSString stringWithFormat:@"%@", file];
NSFileHandle *fh = [NSFileHandle fileHandleForUpdatingAtPath:pathOfFile];
[fh seekToFileOffset:30];
NSData *data = [fh readDataToEndOfFile];

NSRange range = NSMakeRange(30, [data length]-30);
NSData *refinedData = [data subdataWithRange:range];

[fh writeData:nil];
[[NSFileManager defaultManager] createFileAtPath:file contents:refinedData attributes:nil];
于 2012-12-02T20:20:07.203 回答