<分区>
<分区>
Possible Duplicate:
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 上,对于 saveString
,我得到了 null
。
我在 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.txt`
谁能帮我解决这个错误??我想不通。感谢和问候。
最佳答案
好的,那么我用来从 .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;
}
希望对你有帮助
快乐编码:)
关于iphone - 从 .txt 文件中读取 - Objective C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11306689/