![](/img/trans.png)
[英]NSInputStream read: maxlength: returning way more bytes than maxlength
[英]NSInputStream read:maxLength: cannot read data, returns -1
在目標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");
}
我將nsstring
轉換為nsinputstream
,然后嘗試讀取內容
但是在調試代碼后, count
包含-1,即讀取時發生錯誤。 我不明白我在做什么錯! 有人可以幫我嗎。
注意 : Test.txt
是純文本文件,內容僅為“ Hello”,並且調試顯示Str
已使用內容初始化。 這意味着在try
塊中出了一些問題。
任何幫助表示贊賞。
您沒有打開流。 您無法讀取/寫入NSStream
( NSInputStream
和NSOutputStream
是兩個子類NSStream
無需先打開它)。 在從流對象讀取之前,只需對其調用open
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.