[英]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. 您无法读取/写入
NSStream
( NSInputStream
和NSOutputStream
是两个子类NSStream
无需先打开它)。 Just call open
on your stream object prior to reading from it. 在从流对象读取之前,只需对其调用
open
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.