10

I cant seem to get nsdata to write to a file. Any ideas what i may be doing wrong. Thanks in advance.

NSString* filename = @"myfile.txt";

NSString *applicationDocumentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *storePath = [applicationDocumentsDir stringByAppendingPathComponent:filename];    

if ([fileManager fileExistsAtPath:applicationDocumentsDir])
    NSLog(@"applicationDocumentsDir exists");   // verifies directory exist

NSData *data = [NSData dataWithContentsOfURL:URL];

if (data) {    
     NSString *content = [[NSString alloc]  initWithBytes:[data bytes]
                                                      length:[data length] encoding: NSUTF8StringEncoding];

    NSLog(@"%@", content); // verifies data was downloaded correctly

    NSError* error;
    [data writeToFile:storePath options:NSDataWritingAtomic error:&error];

    if(error != nil)
        NSLog(@"write error %@", error);
}

I keep getting the error

"The operation couldn’t be completed. No such file or directory"
Caleb
  • 120,112
  • 19
  • 171
  • 259
user346443
  • 4,338
  • 13
  • 53
  • 75
  • 6
    You should not check whether `error` is nil or not -- It could be initialized to anything (in your case). Check the result of `writeToFile:options:error` to determine success or failure. If it returns false (NO), only then should you read error. – Chaitanya Gupta Aug 29 '11 at 17:24
  • Thanks for the response. It returns NO. I removed the check to make the post as short as possible. – user346443 Aug 29 '11 at 17:45
  • What is the value of your storePath variable before you call writeToFile:options:error: ? Your code looks good, but the error you're getting makes me question where that path is pointing. – Sam Aug 29 '11 at 18:09

2 Answers2

5

Try

NSString *storePath = [applicationDocumentsDir stringByAppendingPathComponent:@"myfile.txt"];

And

 if ([[NSFileManager defaultManager] fileExistsAtPath:storePath])
        NSLog(@"applicationDocumentsDir exists");   
  • 1
    So that worked, weird. What would a string be any different from a nsstring variable. – user346443 Aug 29 '11 at 18:14
  • If you print it out and it has file: at the beginning, it's an URL. I just came across this, even though I'm sticking with the string functions. – Stephen J May 09 '17 at 16:32
  • fileExistsAtPath returns false. What to do? Of cause it's not exist cause file is not created yet. How to create it? – NikeAlive Oct 12 '17 at 07:13
0

To get more information, you can use

writeToFile:options:error:

instead of

writeToFile:atomically:

but you need to create all the subdirectories in the path prior to doing the write. Like this:

// if the directory does not exist, create it...
if ( [fileManager fileExistsAtPath:dir_path] == NO ) {
    if ( [fileManager createDirectoryAtPath:dir_path withIntermediateDirectories:NO attributes:NULL error:&error] == NO  ) {
        NSLog(@"createDirectoryAtPath failed %@", error);
    }
}
Jeff
  • 2,395
  • 20
  • 39