从NSData读取整数?
我想我在这里有点困惑,我所拥有的是一个纯文本文件,其中包含数字“5 10 2350”。正如你可以在下面看到的,我正在尝试使用readDataOfLength读取第一个值,我想也许在那里我变得混乱的是,我应该阅读为字符,但是然后10是2个字符和2350是4。正确的方向来阅读这些。从NSData读取整数?
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
从贾勒特和OLE兼具优良的答案,这里是我已经用。最后一个问题“方法02”选择回车到文本文件底部的空行,将其作为一个子字符串返回,然后转换为“0”,我可以设置NSCharacterSet来停止它,目前我只是在字符串上添加了一个长度检查。
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]);
}
}
加里
有几种便捷的可可,可以让你在这里的生活更容易一点:
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);
}
}
否则,使用原始的方法,你几乎必须阅读逐个字符,将每个字符添加到“缓冲区”,然后在遇到空间(或换行符或其他分隔符)时评估整数。
如果你读了文件的内容到一个字符串作为Jaret suggested,并假设该字符串仅包含数字和空格,您也可以拨打:
NSArray *substrings = [s componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
这将在空格和换行字符和回报分割字符串子串的数组。然后您必须通过在数组上循环并调用[substring integerValue]
将子字符串转换为整数。
一个不错的和简单的方法 – 2009-12-10 14:09:14
根据您的使用情况,这可能会非常缓慢。使用NSScanner(如上所述)将比这种阵列方法快一个数量级。但对你来说可能并不重要。 – walkingbrad 2015-05-01 17:30:19
一种方法做如下这将是第一个到第一次打开您readBuffer成字符串:
NSString * dataString = [[NSString alloc] initWithData:readBuffer encoding:NSUTF8StringEncoding];
然后将字符串分割成值:
这NSString *[email protected]"5 10 2350"; // example string to split
NSArray * valueStrings = [dataString componentsSeparatedByString:@" "];
for(NSString *valueString in valueStrings)
{
int value=[valueString intValue];
NSLog(@"%d",value);
}
输出是
5
10
2350
嗯....我似乎无法重复方法02中的尾随0只是换行...但是,如果我把一个非数字字符在最后。 – 2009-12-10 15:23:46
也许我的测试文件中隐藏着某些东西,我正在拾取。无论如何感谢您的反馈。 – fuzzygoat 2009-12-11 16:41:23