简体   繁体   English

NSInputStream read:maxLength:无法读取数据,返回-1

[英]NSInputStream read:maxLength: cannot read data, returns -1

In Objective C, I have somethng like this: 在目标C中,我有这样的东西:

uint8_t f[400000];
NSString *Str;
unsigned int count = 0;
NSError* error = nil;
Str = [[NSString alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Test" ofType:@"txt"] encoding:NSUTF8StringEncoding error:&error];
@try {
    NSInputStream *fin = [NSInputStream inputStreamWithData:[Str dataUsingEncoding:NSUTF8StringEncoding]];
    count = [fin read:f maxLength:400000];

}
@catch (NSException * e) {
    NSLog(@"Exception: %@", e);
}
@finally {
    NSLog(@"finally");
}

I am converting nsstring to nsinputstream and then trying to read the content 我将nsstring转换为nsinputstream ,然后尝试读取内容

But after I debug the code, count has -1 in it, ie an error occurred while reading. 但是在调试代码后, count包含-1,即读取时发生错误。 I don't understand what am I doing wrong here! 我不明白我在做什么错! Can someone please help me out. 有人可以帮我吗。

NOTE : Test.txt is a plain text file with content as just "Hello" and debugging shows Str is initialised with the content. 注意Test.txt是纯文本文件,内容仅为“ Hello”,并且调试显示Str已使用内容初始化。 That means something is wrong in the try block. 这意味着在try块中出了一些问题。

Any help is appreciated. 任何帮助表示赞赏。

You didn't open the stream. 您没有打开流。 You cannot read from/write to a NSStream ( NSInputStream and NSOutputStream are both subclasses of NSStream ) without opening it first. 您无法读取/写入NSStreamNSInputStreamNSOutputStream是两个子类NSStream无需先打开它)。 Just call open on your stream object prior to reading from it. 在从流对象读取之前,只需对其调用open

暂无
暂无

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

相关问题 NSInputStream读取:maxlength:返回的字节数比maxlength多 - NSInputStream read: maxlength: returning way more bytes than maxlength NSInputStream:read:maxLength:在TCP套接字流上返回错误“操作无法完成。 错误的文件描述符” - NSInputStream:read:maxLength: on a TCP socket stream returns the error “The operation couldn’t be completed. Bad file descriptor” 当-[NSInputStream read:maxLength:]返回0时,它在Apple Document中是否模棱两可? - Is it ambiguous in Apple Document when -[NSInputStream read:maxLength:] return 0? - [NSInputStream read:maxLength:]抛出一个异常,说长度太大,但事实并非如此 - -[NSInputStream read:maxLength:] throws an exception saying length is too big, but it isn't 当可用字节时,NSInputStream读取返回无符号整数最大值 - NSInputStream read returns unsigned integer maximum value when bytes available 如何将NSInputStream转换为NSString或如何读取NSInputStream - How to convert NSInputStream to NSString or how to read NSInputStream 将char []写入NSOutputStream并从NSInputStream读取 - write char[] to NSOutputStream and read from NSInputStream 无法从NSInputStream读取自定义文件 - Can't read custom file from NSInputStream NSInputStream包装器,用于通过指定的分隔符读取字符串 - NSInputStream wrapper to read strings by specified delimiter 使用 NSInputStream 从文件中读取数字 - Read numbers from file using NSInputStream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM