簡體   English   中英

使用 NSInputStream 從文件中讀取數字

[英]Read numbers from file using NSInputStream

我正在嘗試使用 NSInputStream 從目標 C 中的文件中讀取一些輸入數字。 但無法這樣做,我不想使用 readContentsFromFile api,而是使用 NSInputStream。 請建議如何這樣做。

我正在關注的重要事項:
1. 如何從文件中讀取整數。
2.如何將uint_8 []中的數據轉換為integer。
3. NSInputStream 一次應該讀取多大的數據?

Example.txt 文件:

20 30 40 50 60 70 80 90 100 120 140 160 180 190 20 30 40 50 60 70 80 90 100 120 140 160 180 190 20 30 40 50 60 70 80 90 100 120 140 160 180 190 20 30 40 50 60 70 80 90 100 120 140 160 180 190 20 30 40 50 60 70 80 90 100 120 140 160 180 190


NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Example" ofType:@"txt"];
NSInputStream *stream = [NSInputStream inputStreamWithFileAtPath:filePath];
 [stream open];
 for(int i = 0; i < 20; i ++)
 {
    if(stream && [stream hasBytesAvailable])
    {
        uint8_t buffer[1024];
        NSUInteger len = [stream read:buffer maxLength:32];
        if(len>0)
        {
            NSLog(@"%ld",buffer[0]);
        }
        else
        {
            ....
        }
    }
 }

您應該實現NSStreamDelegate方法來使用NSInputStream讀取文件。 這就是在這種情況下你應該如何初始化你的NSInputStream實例:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Example" ofType:@"txt"];
NSInputStream *stream = [NSInputStream inputStreamWithFileAtPath:filePath];
[stream setDelegate:self]; // or any other object conforming to NSStreamDelegate
[stream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[stream open];

在用於讀取文件的 class 中聲明以下屬性:

@interface MyClassToReadFile: NSObject<NSStreamDelegate>

@property (strong, nonatomic) NSMutableData* data;
@property (nonatomic) NSInteger bytesRead;

@end

這是實現stream:handleEvent:的方法:

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
    switch (eventCode) {
    case NSStreamEventHasBytesAvailable: {
        if(!_data) {
            _data = [NSMutableData data];
        }
        uint8_t buf[1024]; // or any other size
        NSInteger len = [(NSInputStream *)stream read:buf maxLength:sizeof(buf)/sizeof(buf[0])];
        if(len) {
            [_data appendBytes:(const void *)buf length:len];
            _bytesRead += len;
        }
        break;
    }
    case NSStreamEventEndEncountered: {
        [stream close];
        [stream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        NSString* fileData = [[NSString alloc] initWithData:_data encoding:NSASCIIStringEncoding];
        NSArray<NSString*>* numbersAsStrings = [fileData componentsSeparatedByCharactersInSet:NSCharacterSet.whitespaceCharacterSet];
        NSMutableArray<NSNumber*>* numbers = [NSMutableArray array];
        for (NSString* numberAsString in numbersAsStrings) {
            [numbers addObject:[NSNumber numberWithLongLong:numberAsString.longLongValue]];
        }
        for (NSString* numberAsString in numbersAsStrings) {
            NSLog(@"%lld", numberAsString.longLongValue);
        }
        break;
    }
    default:
        break;
    }
}

暫無
暫無

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

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