tags:

views:

343

answers:

3

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

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

gary

+3  A: 

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).

Jarret Hardie
+3  A: 

If you read the file's contents into a string as Jaret suggested, and assuming the string only contains numbers and whitespace, you can also call:

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

This will split the string at whitespace and newline characters and return an array of the substrings. You would then have to convert the substrings to integers by looping over the array and calling [substring integerValue].

Ole Begemann
a nice and simple approach
Jarret Hardie
A: 

One way to do it would be first to first turn your readBuffer into a string as follows:

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

Then split the string into values:

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

Output of this is

5
10
2350
invariant