[英]Can't read custom file from NSInputStream
I am trying to read a custom file from my documents directory via NSInputStream to upload it to a FTP server. 我正在尝试通过NSInputStream从我的文档目录中读取自定义文件,以将其上传到FTP服务器。 I'm using the exact same code as demonstrated in the SimpleFTPSample provided by Apple.
我使用的代码与Apple提供的SimpleFTPSample中演示的代码完全相同。 It seems to work fine as long as the file is empty, but as soon as it contains data it fails.
只要文件为空,它似乎可以正常工作,但是一旦包含数据,它就会失败。
Here's my code. 这是我的代码。 This is how I create the input stream, I tried it both ways:
这是我创建输入流的方式,我尝试了两种方式:
//Approach one: Init with path
self.fileStream = [NSInputStream inputStreamWithFileAtPath:filePath];
//Approach two: Init with data
NSData* fileData = [NSData dataWithContentsOfFile:filePath];
self.fileStream = [NSInputStream inputStreamWithData:fileData];
If I init the stream with data, I get EXC_BAD_ACCESS (code=1, address=0x0)
when invoking read
on the stream (code snipped below), if I use the path it jumps right to File read error
. 如果我使用数据初始化流,则调用流上的
read
(下面的代码如下)时,会得到EXC_BAD_ACCESS (code=1, address=0x0)
),如果我使用的路径将其跳转到File read error
。
filePath
is @"/var/mobile/Applications/94292A2A-37FC-4D8E-BDA6-A26963932AE6/Documents/1395576645.cpn"
, NSData
returns properly and has 806 bytes. filePath
为@"/var/mobile/Applications/94292A2A-37FC-4D8E-BDA6-A26963932AE6/Documents/1395576645.cpn"
, NSData
正确返回并具有806个字节。
That's part of the stream:handleEvent:
delegate method: 那是
stream:handleEvent:
委托方法的一部分:
if (self.bufferOffset == self.bufferLimit) {
NSInteger bytesRead;
bytesRead = [self.fileStream read: self.buffer maxLength: kSendBufferSize];
if (bytesRead == -1) {
if (kPrintsToConsole) NSLog(@"File read error");
} else if (bytesRead == 0) {
[self stopSending];
} else {
self.bufferOffset = 0;
self.bufferLimit = bytesRead;
}
}
I'm kinda stuck. 我有点卡住了。 Hope you guys can help me out.
希望你们能帮助我。 Running iOS 7.1 & Xcode 5.1
运行iOS 7.1和Xcode 5.1
you need to call [self.fileStream open] before read. 您需要在读取之前调用[self.fileStream open]。 For both file and data approaches.
对于文件和数据方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.