WriteToFile does not write file

Well, I am having a problem and I have been struggling with it for several days. Here it is: when I try to write to the xml file (placed in the xcode project that I am developing) using writeToFile, the write does not work and I don’t see anything in the XML file, although the bool value that is returned from writeToFile is evaluated to the truth !! In addition, file bytes are zero. So I would really appreciate if anyone could help me. The following is part of the code 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 either.

 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"); } 
+4
source share
4 answers

In the first code snippet, we cannot see the definition of path . If this is a typo, and you meant file_path , the problem is that file_path points to the path inside the application package. You cannot write in your application. (There should not be any typos, because you have to embed the code directly.)

In the second case, it is more difficult to say what the problem is. filePath must be in a document directory that can be written. However, it would be much easier to diagnose the problem if you received an actual error. Instead of passing nil for the error parameter to -writeToFile:atomically:encoding:error: create an NSError* variable and navigate to its address. If there is a problem, then your pointer will be set to an NSError object that describes the problem.

+3
source

The fact that writeToFile: returns a boolean value of YES just means that the call ends.

You should pass NSError** to writeToFile: and examine this, for example:

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

This should give you a good idea of ​​what is going wrong (if the error is not zero after the call).

+3
source
 -(void)test{ //writing to a file NSError *error; NSString *pathOfFile = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@".xml"];//was missing '.' NSString *dummyString = @"Hello World!!\n"; NSFileManager *filemanager; filemanager = [NSFileManager defaultManager]; //entering the first condition so I assured that the file do exists //the file exists in the bundle where you cannot edit it if ([filemanager fileExistsAtPath:pathOfFile]){ NSLog (@"File do exist IN Bundle MUST be copied to editable location!!!"); NSArray *locs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docsDir = [[locs objectAtIndex:0]stringByAppendingString:@"dummyFile"]; NSString *file = [docsDir stringByAppendingPathComponent:@".xml"]; [filemanager copyItemAtPath:pathOfFile toPath:file error:&error]; } else{ NSLog (@"File not found in bundle!!!"); } if (error) { NSLog(@"error somewhere"); } //if youre only messing with strings you might be better off using .plist file idk BOOL success = [dummyString writeToFile:file 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(!success) { NSLog(@"Error: Could not write to the file"); } } 
0
source
 //I typically do something like this //lazy instantiate a property I want such as array. -(NSMutableArray*)array{ if (!_array) { _array = [[NSMutableArray alloc]initWithContentsOfFile:self.savePath]; } return _array; } //I'm using a class to access anything in the array from as a property from any other class //without using NSUserDefaults. //initializing the class -(instancetype)init{ self = [super init]; if (self) { //creating an instance of NSError for error handling if need be. NSError *error; //build an array of locations using the NSSearchPath... NSArray *locs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //location 0 of array is Documents Directory which is where I want to create the file NSString *docsDir = [locs objectAtIndex:0]; //my path is now just a matter of adding the file name to the docsDir NSString *path = [docsDir stringByAppendingPathComponent:@"fileName.plist"]; //if the file is a plist I have in my bundle I need to copy it to the docsDir to edit it //I'll do that by getting it from the bundle that xCode made for me NSString *bunPath = [[NSBundle mainBundle]pathForResource:@"fileName" ofType:@".plist"]; //now I need a NSFileManager to handle the dirty work NSFileManager *filemngr = [NSFileManager defaultManager]; //if the file isn't in my docsDir I can't edit so I check and if need be copy it. if (![filemngr fileExistsAtPath:path]) { [filemngr copyItemAtPath:bunPath toPath:path error:&error]; //if an error occurs I might want to do something about it or just log it as in below. //I believe you can set error to nil above also if you have no intentions of dealing with it. if (error) { NSLog(@"%@",[error description]); error = nil; } //here I'm logging the paths just so you can see in the console what going on while building or debugging. NSLog(@"copied file from %@ to %@",bunPath,path); } //in this case I'm assigning the array at the root of my plist for easy access _array = [[NSMutableArray alloc]initWithContentsOfFile:path]; _savePath = path;//this is in my public api as well for easy access. } return self; } 
0
source

All Articles