ios - 创建一个特定大小的空文件并使用 fseek 将新数据写入特定位置

标签 ios nsdata fseek nsfilehandle nsoutputstream

我是ios新手 我有 10 个 1024 字节的 block 。这些 block 不按顺序排列。 如果我知道 block 的数量,如何将数据插入大小为 10*1024 的文件中? 到目前为止我有以下代码:

//method called when new chunk arrvies


 NSArray *paths = NSSearchPathForDirectoriesInDomains
    (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    //create a file name to write the data to using the documents directory:
    filePath = [NSString stringWithFormat:@"%@/example.txt", 
                documentsDirectory];

    NSFileHandle *writeFile = [NSFileHandle fileHandleForWritingToURL:filePath error:nil];
    [writeFile truncateFileAtOffset:10240*sizeof(Byte)];

如何使用fseek添加新数据?

我确实尝试使用以下代码:

if ((chunkNo == 0 )||(chunkNo == 10))
    {
        NSOutputStream *stream = [[NSOutputStream alloc] initToFileAtPath:filePath append:YES];
        [stream open];
        NSData *chunk = chunkContainer; // some data
        [stream write:(uint8_t *)[chunk bytes] maxLength:[chunk length]];
        [stream close];

    }
    else{
        NSData *chunk = chunkContainer; // some data
        NSUInteger insertPoint = chunkNo*1024; // get the insertion point //
        // make sure the file exists, if it does, do the following //
        NSData *oldData = [NSData dataWithContentsOfFile:filePath];

        NSOutputStream *stream = [[NSOutputStream alloc] initToFileAtPath:filePath append:NO];
        [stream open];
        [stream write:(uint8_t *)[oldData bytes] maxLength:insertPoint]; // write the old data up to the insertion point //
         [stream write:(uint8_t *)[chunk bytes] maxLength:[chunk length]]; // write the new data //
         [stream write:(uint8_t *)([oldData bytes]+insertPoint) maxLength:([oldData length]-insertPoint)]; // write the rest of old data at the end of the file //

        [stream close];
    }

但是,如果 block 的顺序不正确,则结果文件不完全正确。如果 block 的顺序正确,则结果文件将正确显示。

最佳答案

示例:

NSString *filePath = [@"~/Desktop/FHTest.txt" stringByExpandingTildeInPath];

[[NSFileManager defaultManager] createFileAtPath:filePath contents:[NSData new] attributes:nil];
NSFileHandle *fh;

NSData *data1 = [@"test Data" dataUsingEncoding:NSUTF8StringEncoding];
fh = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
[fh writeData:data1];
[fh closeFile];

NSData *data2 = [@"more Data" dataUsingEncoding:NSUTF8StringEncoding];
fh = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
[fh seekToFileOffset:100];
[fh writeData:data2];
[fh closeFile];

请注意,seekToFileOffset:100 将根据需要填充扩展文件,并根据需要填充 0x00 字节。

要创建特定大小的文件,在本例中为 100 字节:

unsigned long long size = 100;
[[NSFileManager defaultManager] createFileAtPath:filePath contents:[NSData new] attributes:nil];
NSFileHandle *fh = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
[fh seekToFileOffset:size-1];
[fh writeData:[@"\x00" dataUsingEncoding:NSUTF8StringEncoding]];
[fh closeFile];

关于ios - 创建一个特定大小的空文件并使用 fseek 将新数据写入特定位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24386858/

相关文章:

ios - swift 4 中的 SideMenu 功能

objective-c - 'NSMutableArray' 没有可见的 @ 接口(interface)声明选择器 'appendData:'

ios - QuickBlox ios 音频文件上传问题

c - 电脑是怎么算的?

c++ - 如何在 iOS 中添加带有 PCM 数据/缓冲区的可播放(例如 wav、wmv) header ?

iphone - 应用首次加载时如何显示视频?

ios - 使用 Storyboard的 UIPageViewController 示例

ios - NativeScript:从 interop.reference 获取字符串

c++ - fseek 写入值时回到文件末尾

C 以二进制模式读/写文件