简体   繁体   English

如何从NSFileHandle *获取FILE *?

[英]How to get FILE * from NSFileHandle *?

An old parser relies on a FILE * to work. 旧的解析器依靠FILE *来工作。 However, the Dropbox Sync API for iOS returns a NSFileHandle * instead of a FILE * as a file handle. 但是,适用于iOS的Dropbox Sync API返回NSFileHandle *而不是FILE *作为文件句柄。

So I try to use fileDescriptor of a NSFileHandle : 所以我尝试使用NSFileHandle fileDescriptor

- (NSFileHandle )readHandle:(DBError * )error -(NSFileHandle )readHandle:(DBError * )错误

Returns a read-only file handle for the file. 返回文件的只读文件句柄。 If the file is not cached then the method will block until the file is downloaded. 如果未缓存文件,则该方法将阻塞,直到下载文件为止。 Returns 退货

A file handle if the file can be read, or nil if an error occurred. 如果可以读取文件,则为文件句柄;如果发生错误,则为nil。

and passed as a FILE * : 并作为FILE *传递:

- (void)loadDBFile:(DBFile *)dbFile
{
    DBError *dbError;
    NSFileHandle *fileHandle = [dbFile readHandle:&dbError];
    if (fileHandle) {
         FILE *file = fileHandle.fileDescriptor;
         fseek(file, 0, SEEK_END); // EXE_BAD_ACCESS here!
         // ...
    }
}

However, there is an EXE_BAD_ACCESS at the fseek line. 但是,在fseek行上有一个EXE_BAD_ACCESS

fileDescriptor returns the underlying file descriptor, a simple integer, not a FILE object (which wraps a file descriptor, but isn't the same thing). fileDescriptor返回基础文件描述符,一个简单的整数,而不是FILE对象(它包装了文件描述符,但不是同一对象)。 Use fdopen() to obtain a FILE object from the file descriptor: 使用fdopen()从文件描述符获取FILE对象:

FILE *file = fdopen([filehandle fileDescriptor], "r"); // Create a read-only FILE object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM