我正在尝试打开一个小文本文件,以测试该文件上的一些NSFileHandle函数。然而,我不知道该怎么做,如果你能告诉我我错过了什么,那就太好了。
//.h
@interface MainViewController : UIViewController <FlipsideViewControllerDelegate> {
NSFileHandle *nCode;
}
@property (nonatomic, retain) IBOutlet NSFileHandle *nCode;
- (IBAction)showInfo:(id)sender;
//check for code
- (void)fetchCode:(id)sender; //this function is attached to a button in the .nib
@end//.m
- (void)fetchCode:(id)sender{
nCode = [NSFileHandle fileHandleForReadingAtPath:@"nCode01.txt"];
if (nCode == nil) {
NSLog (@"Open of nCode for reading failed\n");
return;
}
[nCode closeFile];
}我在这里所做的只是试图打开文件,但是每次我按下按钮时,我都会收到错误消息"Open of nCode for reading“……我做错了什么?
从这里我希望找到一种方法,让用户输入一个数字,代表文本文件中的一行,然后返回该行中的文本。有谁知道这样做的方法吗?有可能吗?
发布于 2011-06-15 06:56:28
最有可能的问题是文件路径,因为您没有指定完整的路径名。当您想要访问应用程序包中的文件时,请使用-[NSBundle pathForResource:ofType]
NSString *path = [[NSBundle mainBundle] pathForResource:@"nCode01"
ofType:@"txt"];
nCode = [NSFileHandle fileHandleForReadingAtPath:path];https://stackoverflow.com/questions/6350988
复制相似问题