iphone scanning a dat file for data
I am trying to remake a pro开发者_JAVA百科gram I have made in C# in OBJ-C.In C# I used streamreader to search the data file for the line I am looking for then convert that line into a string that I can work with.
I have looked at NSScanner but I'm not sure if thats quite waht I'm looking for but I'm by no means a cocoa expert.
All I would like to be able to do is have it search a data file for an occurance of a string, then when/if it finds an occurance of that string, it returns the line that string was found on as a string.
Any ideas?
If your data file isn't to large to fit in memory, you can just load it into a string and search it using string methods. For example:
NSData *data = [NSData dataWithContentsOfFile:@"/path/to/file.dat"];
NSString *dataString = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
for (NSString *line in [dataString componentsSeparatedByString:@"\n"])
if (!NSEqualRanges([line rangeOfString:searchString], NSMakeRange(NSNotFound,0)))
return line;
精彩评论