从NSData读取内容?

时间:2009-12-10 13:40:39

标签: objective-c

我想我在这里有点困惑,我所拥有的是一个纯文本文件,其中包含数字“5 10 2350”。正如你在下面看到的那样,我试图使用readDataOfLength读取第一个值,我想也许我变得糊涂的地方是我应该读作chars,但是10是2个字符而2350是4.任何人都可以指向m阅读这些的正确方向。

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”将回车符号返回到文本文件底部的空白行,将其作为subString返回,然后转换为“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]);
    }
}

加里

3 个答案:

答案 0 :(得分:4)

Cocoa有几种便利可以让你的生活更轻松:

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

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

答案 1 :(得分:3)

如果您将文件的内容读作字符串Jaret suggested,并假设字符串只包含数字和空格,您也可以调用:

NSArray *substrings = [s componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

这会将字符串拆分为空格和换行符,并返回子串的数组。然后,您必须通过循环遍历数组并调用[substring integerValue]将子字符串转换为整数。

答案 2 :(得分:0)

一种方法是首先将readBuffer转换为字符串,如下所示:

NSString * dataString = [[NSString alloc] initWithData:readBuffer encoding:NSUTF8StringEncoding];

然后将字符串拆分为值:

NSString *dataString=@"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