Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writeToFile is not writing to the file

Well I am experiencing a problem and I've been struggling with it for few days. Here it is: when trying to write to an xml file (placed in the xcode project which I am developing) using writeToFile, writing doesn't work and I can see nothing in the xml file although the bool value which is returned from writeToFile is being evaluated to true !! In addition, the file bytes are zero. So, I would really appreciate if anyone can help me out with that. Below is part of the code which I wrote:

//writing to a file
 NSString *pathOfFile = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"xml"];
 NSString *dummyString = @"Hello World!!\n"; 
 NSFileManager *filemanager;

 filemanager = [NSFileManager defaultManager];

  //entering the first condition so I assured that the file do exists
  if ([filemanager fileExistsAtPath:pathOfFile] == YES)
     NSLog (@"File do exist !!!");
   else
     NSLog (@"File not found !!!");


BOOL writingStatus = [dummyString writeToFile:path atomically:YES encoding:NSUnicodeStringEncoding error:nil];

    //Not entering this condition so I am assuming that writing process is successful but when I open the file I can't find the string hello world and file size shows 0 bytes
    if(!writingStatus)
    {
        NSLog(@"Error: Could not write to the file");
    }

I also tried this alternative, but unfortunately it didn't work too.

NSString *hello_world = @"Hello World!!\n";
NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *directory = [paths objectAtIndex:0];
NSString *fileName = @"sample.xml";
NSString *filePath = [directory stringByAppendingPathComponent:fileName];
NSLog(@"%@",filePath);
BOOL sucess = [hello_world writeToFile:filePath atomically:YES encoding:NSASCIIStringEncoding error:nil];  
if(!sucess)
{
    NSLog(@"Could not to write to the file");
}
like image 844
User897 Avatar asked Oct 03 '11 13:10

User897


2 Answers

In the first piece of code, we can't see the definition of path. If that's a typo and you meant file_path, the problem is that file_path points to a path inside the app bundle. You can't write inside your app bundle. (There shouldn't be any typos, because you should be pasting the code directly.)

In the second case, it's harder to tell what the problem is. filePath should be in the documents directory, which is writeable. However, it'd be a lot easier to diagnose the problem if you were getting an actual error. Instead of passing nil for the error parameter in -writeToFile:atomically:encoding:error:, create a NSError* variable and pass in its address. If there's a problem, then, your pointer will be set to point to an NSError object that describes the problem.

like image 55
Caleb Avatar answered Oct 04 '22 13:10

Caleb


The fact that writeToFile: is returning a boolean value of YES simply means that the call is completing.

You should be passing an NSError** to writeToFile: and examining that, e.g:

 NSError *error = nil;
 BOOL ok = [hello_world writeToFile:filePath atomically:YES 
                        encoding:NSASCIIStringEncoding error:&error];
 if (error) {
      NSLog(@"Fail: %@", [error localizedDescription]);
 }

That should give you a good clue about what is going wrong (assuming error is not nil after the call).

like image 38
Mark Granoff Avatar answered Oct 04 '22 13:10

Mark Granoff