【问题标题】:Read numbers from file using NSInputStream使用 NSInputStream 从文件中读取数字
【发布时间】:2020-02-09 21:19:20
【问题描述】:

我正在尝试使用 NSInputStream 从目标 C 中的文件中读取一些输入数字。但无法这样做,我不想使用 readContentsFromFile api,而是使用 NSInputStream 。请建议如何这样做。

我正在关注的重要事项:
1. 如何从文件中读取整数。
2.如何将数据从uint_8 []转换为整数。
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
        {
            ....
        }
    }
 }

【问题讨论】:

    标签: objective-c objective-c++ nsinputstream


    【解决方案1】:

    您应该实现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];
    

    在用于读取文件的类中声明以下属性:

    @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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-10
      • 1970-01-01
      • 1970-01-01
      • 2015-07-13
      相关资源
      最近更新 更多