Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Buhussain

macrumors newbie
Original poster
Apr 18, 2012
4
0
hi all, am programing a simple app, i wanted to read the content of a file uisng the code:
Code:
 NSFileManager *filemgr;
        NSString *currentPath;
        
        filemgr =[NSFileManager defaultManager];
        currentPath = [filemgr currentDirectoryPath];
        
        NSString *name = [currentPath stringByAppendingPathComponent:@"sample.txt"];

        NSError *error = nil;

        
        NSString* content = [NSString stringWithContentsOfFile:name
                                                      encoding:NSUTF8StringEncoding
                                                         error:&error];
      if(error) { // If error object was instantiated, handle it.
          NSLog(@"ERROR while loading from file: %@", error);
            // …
      }

       NSLog(@"%@",content);

the error i get is : Error Domain=NSCocoaErrorDomain Code=260 "The file “sample.txt” couldn’t be opened because there is no such file."

now am sure that the file is there.

plz help
 
Last edited by a moderator:
Why are you so sure the file is there? If you have evidence, provide it. If you don't have evidence, then you only have belief. Maybe your belief is wrong, even though it may be a strongly held belief.

What is the exact path of the file? NSLog it, and then check for the actual presence of the file at that path. This is an example of collecting evidence.


Also, please identify whether you're running the code on the simulator or an actual device.
 
the file path

the file on my mac would be.
/Users/apple/Documents/Projects/ios projects/sample.txt

i would also to upload my app to the app store, do i need to make changes to the code.
 
the file on my mac would be.
/Users/apple/Documents/Projects/ios projects/sample.txt

i would also to upload my app to the app store, do i need to make changes to the code.

Is that file actually part of your project. It needs to be so that it can be included with the other resources into the compiled result.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.