How to read data from text file and search for a word in the file in iPhone

// get text file path from application bundle.

_string_filePath = [[NSBundle mainBundle] pathForResource:@”fileName” ofType:@”txt”];

 // get the contents of file.

_string_myFileText = [NSString stringWithContentsOfFile:_string_filePathencoding:NSUTF8StringEncoding error:nil];

 // pass the range of text within which the search has to be done. Here it is from the start of file to the end of file.

NSRange range = NSMakeRange(0, _string_myFileText.length);

 //pass the word to search as below. Here it is “string”.

range = [_string_myFileText rangeOfString:@”string”
options:NSCaseInsensitiveSearch range:rangelocale:nil];

  while (range.location != NSNotFound) 

  {

// if word found traverse from the start point till the found location 

   for(int k = 0; k < range.location ; k++) {

           // read file one character at a time

  unichar ch = [_string_myFileText characterAtIndex:k];

        // store characters into a string

 NSString* tempString = [NSString stringWithCharacters:&ch length:1];

_finalString = [_finalString stringByAppendingString:tempString];

}

 // check further in the file whether there is more than one match available or not

range = NSMakeRange(range.location+range.length, _string_myFileText.length-(range.location+range.length));

range = [_string_myFileText rangeOfString:@”string” options:NSCaseInsensitiveSearch range:rangelocale:nil];

}

150 150 Burnignorance | Where Minds Meet And Sparks Fly!