簡體   English   中英

NSInputStream read:maxLength:無法讀取數據,返回-1

[英]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塊中出了一些問題。

任何幫助表示贊賞。

您沒有打開流。 您無法讀取/寫入NSStreamNSInputStreamNSOutputStream是兩個子類NSStream無需先打開它)。 在從流對象讀取之前,只需對其調用open

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM