In Objective C, I have somethng like this:
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
But after I debug the code, count
has -1 in it, ie an error occurred while reading. I don't understand what am I doing wrong here! Can someone please help me out.
: 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.
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. Just call open
on your stream object prior to reading from it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.