Reputation: 11663
I am reading a file in iphone app. Its Works Fine.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"%@",documentsDirectory);
NSString *fileName = [NSString stringWithFormat:@"%@/test.txt",documentsDirectory];
NSLog(@"%@",fileName);
NSString *content = [[NSString alloc] initWithContentsOfFile:fileName
usedEncoding:nil
error:nil];
NSLog (@"%@",content);
}
works good when i am using filename as test.txt
But when i add another file in the resource suppose test1.txt
then NSLog(@"%@",documentsDirectory)
and NSLog(@"%@",fileName)
shows the right result. But
NSLog (@"%@",content);
prints null
in the log
. So what is the reason?
I am printing detail error and it prints
NSFilePath = "/Users/sam-xxx/Library/Application Support/iPhone Simulator/3.2/Applications/51197946-6042-4A90-AA39-F07F8A649308/Documents/test1.txt";
NSUnderlyingError = Error Domain=NSPOSIXErrorDomain Code=2 "Operation could not be completed. No such file or directory";
Upvotes: 0
Views: 1894
Reputation: 158
When you run a app in Simulator, you could copy and paste the right file into the app's Documents directory use Finder, but on iPhone or iTouch how could you paste a file into the Documents directory.
We got only 2 method to put something into Documents directory when app is running on iPhone or iTouch.
in your code, you read a file in the Documents directory, you could be paste a file in the directory manually use Finder. Try this.
NSString *path = [[NSBundle mainBundle] pathForResource:@"test.txt" ofType:nil];
NSString *content = [[NSString alloc] initWithContentsOfFile:path
usedEncoding:nil
error:nil];
NSLog (@"%@",content);
Upvotes: 0
Reputation: 118
It would be best here to check to see if an error is returned:
NSError *error;
NSString *content = [[NSString alloc] initWithContentsOfFile:fileName
usedEncoding:nil
error: &error];
if (error) NSLog(@"Error !: %@", [error localizedDescription]);
That will (hopefully) give you a clue as to whats going on.
(Edited to give example bundle resource usage as the file is in the bundle not the Documents directory).
Docs for NSBundle are here: NSBundle Documentation
You have 2 choices, the one you suggest:
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
Which will return the Bundle resource directory with the filename appended to that path.
Personally I prefer the pathForResource:ofType: method:
NSString *filename = [[NSBundle mainBundle] pathForResource: @"test1" ofType: @"txt"];
As this will not only tell you if the file exists (returns nil if it does not) but will also search the localisation directories if you have them.
Upvotes: 4
Reputation: 2025
Did you check if the file in the error message exists? When run in the simulator you can simply open Finder, press cmd+shift+g and paste the directory to the file.
Maybe you didn't add it to the target? Open the information window for the file in your resources and check which targets are checked.
Upvotes: 0