Can not read to file Iphone

Discussion in 'iOS Programming' started by Buhussain, Mar 8, 2014.

  1. Buhussain, Mar 8, 2014
    Last edited by a moderator: Mar 8, 2014

    Buhussain macrumors newbie

    Apr 18, 2012
    hi all, am programing a simple app, i wanted to read the content of a file uisng the 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
          if(error) { // If error object was instantiated, handle it.
              NSLog(@"ERROR while loading from file: %@", error);
                // …
    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
  2. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    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.
  3. Buhussain thread starter macrumors newbie

    Apr 18, 2012
    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.
  4. JohnsonK macrumors regular

    Mar 6, 2014
  5. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    NSFileManager's currentDirectoryPath is not supported under iOS.

    You should be sure to read the File System Programming Guide, especially the section on "Accessing Files and Directories".
  6. xStep macrumors 68000

    Jan 28, 2003
    Less lost in L.A.
    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.

Share This Page