views:

35

answers:

1

I have a text file that contains a list of coords to construct a path:

123,44

124,67

178,85

This file is included as part of the bundle. The question is how can I read the points from this file. In C++ there's fscanf, what about Cocoa?

+1  A: 

You can use NSString's stringWithContentsOfFile:encoding:error: convenience method, then split the strings by newline, then split them by comma:

NSString *fileContents = [NSString stringWithContentsOfFile:someFilePath encoding:NSUTF8StringEncoding error:NULL];
NSArray *lines = [fileContents componentsSeparatedByString:@"\n"];
for(NSString *line in lines) {
   NSArray *coords = [line componentsSeparatedByString:@","];
   NSInteger x = [[coords objectAtIndex:0] intValue];
   NSInteger y = [[coords objectAtIndex:1] intValue];
   //do something with x and y
}
Jacob Relkin