简体   繁体   English

无法从NSInputStream读取自定义文件

[英]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.

相关问题 无法使用NSInputStream从iOS客户端读取Java服务器的响应 - Can't read response from Java server in iOS client using NSInputStream 如何使用NSInputStream和NSOutputStream读取和写入音频文件 - How to read and write audio file using NSInputStream and NSOutputStream 如何将NSInputStream转换为NSString或如何读取NSInputStream - How to convert NSInputStream to NSString or how to read NSInputStream NSInputStream使用本地文件,而不使用从服务器拉下的文件 - NSInputStream working with with local file, not with file pulled down from server 从文件的特定部分初始化NSInputStream(复制在Document目录中)? - initialising NSInputStream from a particular portion of file (copied in Document directory)? 从NSInputStream读取垃圾 - Garbage reading from NSInputStream 来自NSInputStream的UIImage - UIImage from NSInputStream 如何使用UTF-8读取NSInputStream? - How to read a NSInputStream with UTF-8? 如果[nsInputStream close]被另一个线程调用,是否应该返回[nsInputStream read:…]? - Should [nsInputStream read:…] return if [nsInputStream close] is called by another thread? - [NSInputStream read:maxLength:]抛出一个异常,说长度太大,但事实并非如此 - -[NSInputStream read:maxLength:] throws an exception saying length is too big, but it isn't
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM