Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Reading text file with NSString:stringWithContentsOfFile?

NSString *txtFilePath = [[NSBundle mainBundle] pathForResource:@"help" ofType:@"txt"];
NSString *txtFileContents = [NSString stringWithContentsOfFile:txtFilePath encoding:NSUTF8StringEncoding error:NULL];

NSLog(@"File:  %@",txtFileContents);

I get "null" as the result. How do I know what path to specify?

thanks

(I placed the file just under "Groups and Files" ... so not sure if I need to specify a path or just the file name. Maybe there is something else wrong ???

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
752 views
Welcome To Ask or Share your Answers For Others

1 Answer

I think the backslash in your path is confusing things. Try this:

NSString *txtFilePath = [[NSBundle mainBundle] pathForResource:@"/help" ofType:@"txt"];

Also, as noted in the comments, you need to use the Build Phase of your project to copy the "help.txt" file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...