从.txt文件中读取 - 目标C.

时间:2012-07-03 07:56:28

标签: iphone objective-c ios xcode ipad

  

可能重复:
  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`

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

4 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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);

答案 2 :(得分: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。

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

答案 3 :(得分: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;
}

希望这会对你有所帮助

快乐编码:)