views:

33

answers:

1

I am trying to remake a program 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?

A: 

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], NSRangeMake(NSNotFound,0)))
        return line;
Tom
whats too big? the dat file is 462 kb
Brodie
462 kb is no problem. I wouldn't even worry about anything under 10 MB.
Tom