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

    Joined:
    Apr 18, 2012
    #1
    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
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    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

    Joined:
    Apr 18, 2012
    #3
    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

    Joined:
    Mar 6, 2014
    #4
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    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

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #6
    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