0

可能重复:
使用 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 上,我得到nullsaveString.

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`

有人可以帮我解决错误吗?我无法弄清楚。谢谢并恭祝安康。

4

4 回答 4

1

我认为您的 TXT 文件路径是错误的。检查一下您构建的方式 saveFilePath,也许是:

NSString *savedDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *saveFilePath = [savedDir stringByAppendingPathComponent:savedName];
saveFilePath = [saveFilePath stringByAppendingPathComponent:@"edit.txt"];
于 2012-07-03T08:04:50.383 回答
1

如果你尝试:

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);
于 2012-07-03T08:07:01.987 回答
1

检查您的文档是否包含 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。

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

于 2012-07-03T08:07:12.380 回答
1

好的,那么我用来从 .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;
}

希望对你有帮助

快乐编码:)

于 2012-07-03T08:54:23.950 回答