【问题标题】:Reading From a .txt File - Objective C [duplicate]从 .txt 文件中读取 - 目标 C [重复]
【发布时间】:2012-07-03 15:15:22
【问题描述】:

可能重复:
Read Text file Programmatically using Objective-C

我正在使用以下代码行::

- (void)sync:(NSString *)savedName{

NSLog(@"saved name is this :: %@", savedName);

NSString *savedDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *saveFilePath = [savedDir stringByAppendingPathComponent:savedName];
saveFilePath = [saveFilePath stringByAppendingPathComponent:@"edit.txt"];

NSString *saveString = [NSString stringWithContentsOfFile:saveFilePath encoding:NSUTF8StringEncoding error:nil]; 

NSLog(@"saveString is :: %@ savedName : %@ savefilepath : %@", saveString, savedName, saveFilePath);
}

edit.txt的内容是:

= WWWW =
[[Category:ssss]]

我要做的是从文件edit.txt 中读取并将其内容存储在一个字符串中。但是,在 NSLogging 上,我得到 null 对应的 saveString

我在gdb:::

得到了以下输出
splitView[838:f803] saveString is :: (null) savedName : xxxx savefilepath : /Users/xxxx/Library/Application Support/iPhone Simulator/5.1/Applications/D4B3A4CF-E7D0-4D25-B3D3A170A329/Documents/xxxx/edit.t‌​xt`

有人可以帮我解决错误吗?我无法弄清楚。谢谢和问候。

【问题讨论】:

  • 为什么您的savedName 参数为空?你应该先检查一下。
  • 它不为空.. 它是xxxx
  • 哦,我把 saveName 和 saveString 搞混了,没关系
  • 对不起..但我不认为这是重复的!

标签: iphone objective-c ios xcode ipad


【解决方案1】:

我认为您的 TXT 文件路径有误。看看你构建saveFilePath的方式,也许:

NSString *savedDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *saveFilePath = [savedDir stringByAppendingPathComponent:savedName];
saveFilePath = [saveFilePath stringByAppendingPathComponent:@"edit.txt"];

【讨论】:

  • 哦对不起..我的错..但saveString仍然是空的!我已经编辑了我的问题。
【解决方案2】:

如果你尝试:

NSString *savedDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *saveFilePath = [savedDir stringByAppendingPathComponent:savedName];
//Why do you do that ? saveFilePath = [savedDir stringByAppendingPathComponent:@"edit.txt"];

NSString *saveString = [NSString stringWithContentsOfFile:saveFilePath encoding:NSUTF8StringEncoding error:nil]; 

NSLog(@"saveString is :: %@ savedName : %@ savefilepath : %@", saveString, savedName, saveFilePath);

【讨论】:

  • savedName 的文件夹中有多个 .txt 文件。所以,我尝试使用我的 .txt 文件的特定名称。
  • 我也试过你的代码..仍然是空的。但是,你能解释一下你的第一行是做什么的吗??
  • 当我阅读您的代码时,我怀疑您在查看应用程序的 Document 目录。也许我错了,但如果不是,我的代码在这个目录上更常见。请打印所有路径(其中包含您的文件名)并显示给我们。
  • 我得到以下输出 :: splitView[838:f803] saveString is :: (null) savedName : xxxx savefilepath : /Users/xxxx/Library/Application Support/iPhone Simulator/5.1/Applications/D4B3A4CF-E7D0-4D25-B3D3A170A329/Documents/xxxx/edit.txt
  • 现在用 NSError 变量填充错误参数
【解决方案3】:

检查您的文档是否包含edit.txt文件。

编辑:

- (void)sync:(NSString *)savedName{

    NSString *filePath = [self applicationDocumentsDirectory];
    filePath = [filePath stringByAppendingPathComponent:@"license.txt"];
    NSString *saveString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; 

    NSLog(@"saveString is :: %@ savedName : %@ savefilepath : %@", saveString, savedName, filePath);
}

license.txt 的路径为:/Users/nuzhat.zari/Library/Application Support/iPhone Simulator/5.0/Applications/9BFCE2B5-C976-4EF6-91DF-DA4ABCD04788/Documents。

确保您的文件路径也与此类似。

【讨论】:

  • 是的..它包含`.txt。文件
  • 你能给出你的edit.txt的路径吗?
  • 我已经在我编辑的问题中发布了path,称为savefilePath,以回复NSLog
【解决方案4】:

好的,那么我用来从 .txt 文件中读取数据的内容如下

NSString *path;
path = [[NSBundle mainBundle] pathForResource: YOUR_TEXT_FILE_NAME ofType: @"txt"];

现在一个函数读取返回NSString 作为返回对象的文件。 我对这个函数的调用如下

NSString *data = [self readFile: path]];

现在的功能

-(NSString *)readFile:(NSString *)fileName

{
    NSLog(@"readFile");

    NSString *appFile = fileName;    
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:appFile])        
    {
        NSError *error= NULL;
        NSString *resultData = [NSString stringWithContentsOfFile: appFile encoding: NSUTF8StringEncoding error: &error]; 
        if (error == NULL)            
            return resultData;
    }
    return NULL;
}

希望对你有帮助

快乐编码:)

【讨论】:

  • 我应该如何在这里定义我的路径??在我的代码中.. 我想我已经正确定义了我的路径。
  • 只用这个从文件中读出,不保存任何文件
  • 即仅适用于NSString *saveString = [self readFile: path]];,其中path 与上面在我的答案中给出的相同,只需输入正确的名称而不是YOUR_TEXT_FILE_NAME
  • 请告诉我如何定义路径。我越来越糊涂了。
  • ok path = [[NSBundle mainBundle] pathForResource:edit ofType: @"txt"]; 如果您的文件名是 edit.txt 只需输入 savedName 而不是 edit 因为您使用 savedName 来保存文件。对吗?
猜你喜欢
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 2020-10-09
  • 1970-01-01
  • 2018-09-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多