I'm all for just a little guidance, and thank you for it. Everything you said makes sense now, and I have it working:
NSString *searchStr = @"\nJOY\n"; const char *cString = [searchStr UTF8String]; const char *fileBytes = [stringFileContents bytes]; char *ptr = strstr(fileBytes, cString); Thanks again On Thu, Apr 16, 2009 at 4:43 PM, Greg Guerin <glgue...@amug.org> wrote: > Miles wrote: > > NSString *searchStr = @"\njoy\n"; >> NSData *strData = [searchStr >> dataUsingEncoding:NSUTF8StringEncoding]; >> const char *strBytes = [strData bytes]; >> > > Think about what you're doing here, then look at the NSString method > -UTF8String. > > Also, think about whether dataUsingEncoding: includes or excludes a > terminating null. For example, if searchStr were assigned @"", what is the > length of the resulting strData, and exactly what does [strData bytes] > return? A combination of referring to reference docs and some experimenting > is the usual approach here. > > Next, consider the above in light of the args to strstr(), which are > expected to be null-terminated C-style strings. > > Finally, consider what happens if searchStr isn't found. I urge you to try > it with a searchStr assigned a misspelled word like @"spelchek". > > If it seems like I'm asking you to debug and understand your own code > without asking the list for the complete answer, you're right. This is a > pretty elementary example to work on, and there's no substitute for being > able to figure it out for yourself. If you can't debug this one without > direct assistance, you will be lost at sea when the debugging gets harder > (and it WILL get harder). > > -- GG > > _______________________________________________ > > Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) > > Please do not post admin requests or moderator comments to the list. > Contact the moderators at cocoa-dev-admins(at)lists.apple.com > > Help/Unsubscribe/Update your Subscription: > http://lists.apple.com/mailman/options/cocoa-dev/vardpenguin%40gmail.com > > This email sent to vardpeng...@gmail.com > _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com