从 NSData 读取整数? [英] Reading ints from NSData?

查看:68
本文介绍了从 NSData 读取整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我在这里有点困惑,我拥有的是一个纯文本文件,其中包含数字5 10 2350".正如您在下面看到的,我正在尝试使用 readDataOfLength 读取第一个值,我想也许我感到困惑的地方是我应该将其作为字符读取,但是 10 是 2 个字符,而 2350 是 4.任何人都可以在阅读这些的正确方向.

I think I am getting a little confused here, what I have is a plain text file with the numbers "5 10 2350" in it. As you can see below I am trying to read the first value using readDataOfLength, I think maybe where I am getting muddled is that I should be reading as chars, but then 10 is 2 chars and 2350 is 4. Can anyone point m in the right direction to reading these.

NSString *dataFile_IN  = @"/Users/FGX/Documents/Xcode/syntax_FileIO/inData.txt";
NSFileHandle *inFile;
NSData *readBuffer;
int intBuffer;
int bufferSize = sizeof(int);

inFile = [NSFileHandle fileHandleForReadingAtPath:dataFile_IN];
if(inFile != nil) {
    readBuffer = [inFile readDataOfLength:bufferSize];
    [readBuffer getBytes: &intBuffer length: bufferSize];

    NSLog(@"BUFFER: %d", intBuffer);
    [inFile closeFile];
}

EDIT_001

来自 Jarret 和 Ole 的出色回答,这就是我所采用的.最后一个问题METHOD 02"在文本文件底部选择一个空行的回车,将其作为子字符串返回,然后将其转换为0",我可以设置 NSCharacterSet 来阻止它,目前我刚刚在字符串上添加了长度检查.

EDIT_001

Both excellent answers from Jarret and Ole, here is what I have gone with. One final question "METHOD 02" picks up a carriage return to a blank line at the bottom of the text file, returns it as a subString, which in turn gets converted to "0" can I set the NSCharacterSet to stop that, currently I just added a length check on the string.

NSInteger intFromFile;
NSScanner *scanner;
NSArray *subStrings;
NSString *eachString;

// METHOD 01 Output: 57 58 59
strBuffer = [NSString stringWithContentsOfFile:dataFile_IN encoding:NSUTF8StringEncoding error:&fileError];
scanner = [NSScanner scannerWithString:strBuffer];
while ([scanner scanInteger:&intFromFile]) NSLog(@"%d", intFromFile);


// METHOD 02 Output: 57 58 59 0
strBuffer = [NSString stringWithContentsOfFile:dataFile_IN encoding:NSUTF8StringEncoding error:&fileError];
subStrings = [strBuffer componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
for(eachString in subStrings) {
    if ([eachString length] != 0) {
        NSLog(@"{%@} %d", eachString, [eachString intValue]);
    }
}

加里

推荐答案

Cocoa 中有几个便利可以让您的生活更轻松:

There are several conveniences in Cocoa that can make your life a bit easier here:

NSString *dataFile_IN  = @"/Users/FGX/Documents/Xcode/syntax_FileIO/inData.txt";

// Read all the data at once into a string... an convenience around the 
// need the open a file handle and convert NSData
NSString *s = [NSString stringWithContentsOfFile:dataFile_IN
                                        encoding:NSUTF8StringEncoding 
                                           error:nil];

// Use a scanner to loop over the file. This assumes there is nothing in
// the file but integers separated by whitespace and newlines
NSInteger anInteger;
NSScanner *scanner = [NSScanner scannerWithString:s];
while (![scanner isAtEnd]) {
    if ([scanner scanInteger:&anInteger]) {
        NSLog(@"Found an integer: %d", anInteger);
    }
}

否则,使用您的原始方法,您几乎必须逐个字符地读取,将每个字符添加到缓冲区",然后在遇到空格(或换行符或其他一些分隔符)时评估您的整数).

Otherwise, using your original approach, you'd pretty much have to read character-by-character, adding each character to a "buffer" and then evaluating your integer when you encounter a space (or newline, or some other separator).

这篇关于从 NSData 读取整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆