Подтвердить что ты не робот

Пример для чтения/записи файла с помощью NSFileHandle

Есть ли хорошие примеры для чтения/записи файла в кусках с объективным c? Я новичок в objective-c и iPhone API, вот пример кода, который я написал. Есть ли лучшие способы сделать это?

-(void) performFileOperation
{
    @try {

        NSFileHandle *inputFileHandle;
        NSFileHandle *outputFileHandle;

        //check whether the output file exist, if not create a new one.
        NSFileManager *morphedFileManager;

        outputFileManager = [NSFileManager defaultManager];

        if ([outputFileManager fileExistsAtPath: self.outputFilePath ] == NO)
        {
            NSLog (@"Output file does not exist, creating a new one");
            [outputFileManager createFileAtPath: self.outputFilePath
                                        contents: nil
                                      attributes: nil];
        }

        NSData *inputDataBuffer;

        inputFileHandle = [NSFileHandle fileHandleForReadingAtPath: self.inputFilePath];

        NSAssert( inputFileHandle != nil, @"Failed to open handle for input file" );

        outputFileHandle  = [NSFileHandle fileHandleForReadingAtPath: self.outputFilePath];

        NSAssert( outputFileHandle != nil, @"Failed to open handle for output file" );

        @try{
            // seek to the start of the file
            [inputFileHandle seekToFileOffset: 0];
            [outputFileHandle seekToFileOffset: 0];

            while( (inputDataBuffer = [inputFileHandle readDataOfLength: 1024]) != nil )
            {
                [outputFileHandle writeData: [self.fileWrapper process: inputDataBuffer]];
            }
        }
        @catch (NSException *exception) {
            @throw;
        }
        @finally {
            [inputFileHandle closeFile];
            [outputFileHandle closeFile];
        }        
    }
    @catch (NSException *exception) {
        @throw;
    }
}

При попытке написать следующее исключение:

Failed to process input buffer ( *** -[NSConcreteFileHandle writeData:]: Bad file descriptor )
4b9b3361

Ответ 1

Требуется изменить строку outputFileHandle:

 outputFileHandle  = [NSFileHandle fileHandleForWritingAtPath: self.outputFilePath];