views:

60

answers:

3

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";
+3  A: 

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.

RichB
Yup I am getting ...Error !: Operation could not be completed. (Cocoa error 260.). But why? I am just adding another existing file in resource.
Hmm, not the most informative error. Are you sure that test1.txt is in the documents directory ? If it was included as part of the Bundle you should get its location by calling:NSString *filename = [[NSBundle mainBundle] pathForResource: @"test1" ofType: @"txt"];A quick way to check would be to sue the NSFileManager:NSFileManager *fileManager = [NSFileManager defaultManager];if (![fileManager fileExistsAtPath: filename]){ LOG ERROR HERE !!!! }
RichB
@Rich I added the detailed error in the question itself. You can see.
if (![fileManager fileExistsAtPath: fileName]) {NSLog(@"Error !: %@", [error localizedDescription]); }prints Error !: (null)
@Rich, The first file, which i added to my application is only not showing any problem. After ward reading any other file shows problem.and I am sure text1.txt is in document directory. beacuse NSLog shows it - /Users/samxxx/Library/Application Support/iPhone Simulator/3.2/Applications/51197946-6042-4A90-AA39-F07F8A649308/Documents/test1.txt
As Christian says above its worth checking that the file is added to your target.The error is clear though, the file is not in the Documents directory. Remember though that the Documents directory is not the same as the Bundle directory. Adding a file to your target at build time adds the file to the Bundle directory NOT the Documents directory. The only way to put a file in the Document directory is to create it (or move it from the Bundle) in the app code.I can't say how your first file got into the Documents directory.
RichB
You should only examine the error object if the method fails. Don't assume that you didn't get an error object if the method succeeded; that's not guaranteed. (The main way this can happen is if the method tries one other method, which fails and returns an error, then tries another method, which succeeds.)
Peter Hosey
@Rich, Thanks alot. Can you tell me how thatNSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName]; is the correct code? If not can you suggest me any document where i can get proper things.
I've edited the answer above for NSBundle usage.
RichB
A: 

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.

Christian
I didn't find "text1.txt" in the directory.Target is checked. /Users/samxxx/objc/HelloWorld/test1.txt is the path.
A: 

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.

  1. Create one.
  2. Copy one from the bundle.

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);
Tony