首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >writeToFile未写入文件

writeToFile未写入文件
EN

Stack Overflow用户
提问于 2011-10-03 21:31:45
回答 4查看 15.6K关注 0票数 5

嗯,我正在经历一个问题,我已经挣扎了几天了。如下所示:当尝试使用writeToFile写入xml文件(放置在我正在开发的xcode项目中)时,虽然从writeToFile返回的布尔值被计算为true,但是写入不起作用,并且在xml文件中什么也看不到!!此外,文件字节为零。所以,如果有人能帮我解决这个问题,我将不胜感激。下面是我写的代码的一部分:

代码语言:javascript
复制
//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");
    }

我也尝试过这种方法,但不幸的是它也不起作用。

代码语言:javascript
复制
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");
}
EN

回答 4

Stack Overflow用户

发布于 2011-10-03 21:40:30

在第一段代码中,我们看不到path的定义。如果这是一个拼写错误,并且您指的是file_path,那么问题是file_path指向应用程序包中的路径。你不能在你的应用程序包里写东西。(不应该有任何拼写错误,因为您应该直接粘贴代码。)

在第二种情况下,很难判断问题是什么。filePath应该在可写的documents目录中。然而,如果你得到的是一个实际的错误,那么诊断问题会容易得多。不是为-writeToFile:atomically:encoding:error:中的nil参数传递错误,而是创建一个NSError*变量并传入其地址。如果有问题,那么您的指针将被设置为指向描述问题的NSError对象。

票数 3
EN

Stack Overflow用户

发布于 2011-10-03 21:43:12

writeToFile:返回布尔值YES这一事实仅仅意味着调用正在完成。

您应该向writeToFile:传递一个NSError**并对其进行检查,例如:

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

这应该会给你一个很好的线索,告诉你哪里出了问题(假设错误在调用后不是零)。

票数 3
EN

Stack Overflow用户

发布于 2014-11-15 13:34:24

代码语言:javascript
复制
-(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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7635696

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档