iOS Can not read to file Iphone

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:

chown33

Moderator
Staff member
Aug 9, 2009
8,435
4,441
Pale blue comma
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.
 

Buhussain

macrumors newbie
Original poster
Apr 18, 2012
4
0
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.
 

xStep

macrumors 68010
Jan 28, 2003
2,001
98
Less lost in L.A.
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.