0

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?

Brodie
  • 3,526
  • 8
  • 42
  • 61

1 Answers1

0

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;
jrdioko
  • 32,230
  • 28
  • 81
  • 120
Tom
  • 3,831
  • 1
  • 22
  • 24