Vincent
Vincent

Reputation: 23

Problems reading local text file with [NSString stringWithContentsOfFile...] with Objective-c returns null

This does not seem so complex. I just want to create a string object with the contents of a local text file called "test.txt" which I have placed in the root of my project.

I am trying to load it w/ the following code.

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"txt"];    
NSString *textData = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];


//The below works, but not my above... not sure why.
//textData = @"TEST TEST";


NSLog(@"Some text from a file of %@", textData);
NSLog(@"The length of the string is %lu", [textData length]);

The output from this code is "Some text from a file of (null)" "The length of the string is 0"

I am not sure why this is returning null instead of the contents of my text file. The encoding for the file seems appropriate. Also, the file exists. Am I dragging the file to the wrong location? Do I have to add it to my NSBundle mainBundle in some way?

Thanks!

Upvotes: 2

Views: 7440

Answers (6)

krishna
krishna

Reputation: 69

Drag and drop your test.txt file to Build Phases->copy bundle resources

Upvotes: 0

Joshua
Joshua

Reputation: 1

I was having the exact same problem as you, Vincent. My understanding is that because I am making an console-based application, it doesn't compile and include files within a bundle in the same way that an iOS app would. Therefore, it's not including the file as expected. I was able to get it to work by typing out the full directory location of the file on my hard drive.

Upvotes: 0

Vincent
Vincent

Reputation: 23

All of the answers here helped a little bit. In particular the suggestion to fill in the error: in a way that does not return nil.

Eventually I found that I had to manually copy the .txt file I was trying to open to the same directory as the binary that was compiled. Just copying it into the xcode project did not seem to work for me.

Upvotes: 0

Lily Ballard
Lily Ballard

Reputation: 185653

If you actually fill in the error parameter in +stringWithContentsOfFile:encoding:error: then it would tell you why it's returning nil. You really ought to do that. The likely reason is there is no file at that path (or you don't have permissions to read it).

Upvotes: 6

X Slash
X Slash

Reputation: 4131

Log your filePath to see if it can actually find the file, if not, you probably add your test.txt in a wrong way. To add to main bundle, drag your test.txt into your xcode project, when prompted, just tick Copy items into destination group's folder (if needed) and select Create groups for any added folders.

Upvotes: 0

YuAo
YuAo

Reputation: 1427

NSLog the filePath and see if it's nil. If the filePath is nil, I think you didn't put the file in right place. You should drag the file into the project navigator of your project in Xcode. Also, double check the file name.

Upvotes: 0

Related Questions