2009-12-10 68 views
1

我想我在這裏有點困惑,我所擁有的是一個純文本文件,其中包含數字「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]); 
    } 
} 

加里

+0

嗯....我似乎無法重複方法02中的尾隨0只是換行...但是,如果我把一個非數字字符在最後。 – 2009-12-10 15:23:46

+0

也許我的測試文件中隱藏着某些東西,我正在拾取。無論如何感謝您的反饋。 – fuzzygoat 2009-12-11 16:41:23

回答

4

有幾種便捷的可可,可以讓你在這裏的生活更容易一點:

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

否則,使用原始的方法,你幾乎必須閱讀逐個字符,將每個字符添加到「緩衝區」,然後在遇到空間(或換行符或其他分隔符)時評估整數。

3

如果你讀了文件的內容到一個字符串作爲Jaret suggested,並假設該字符串僅包含數字和空格,您也可以撥打:

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

這將在空格和換行字符和回報分割字符串子串的數組。然後您必須通過在數組上循環並調用[substring integerValue]將子字符串轉換爲整數。

+0

一個不錯的和簡單的方法 – 2009-12-10 14:09:14

+0

根據您的使用情況,這可能會非常緩慢。使用NSScanner(如上所述)將比這種陣列方法快一個數量級。但對你來說可能並不重要。 – walkingbrad 2015-05-01 17:30:19

0

一種方法做如下這將是第一個到第一次打開您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