开发者

how to open text file with NSFileHandle

I am trying to open a small text file to test some NSFileHandle functions on the file. however I cannot figure out how to do this, if you could tell me what I am missing that would be great.

//.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:@"nCode开发者_运维技巧01.txt"];

if (nCode == nil) {
    NSLog (@"Open of nCode for reading failed\n");
    return;
}

 [nCode closeFile];
}

all I am doing here is trying to open the file, however each time I press the button I recive the error message "Open of nCode for reading failed"... what am I doing wrong ?

from this I am hoping to find a way to let the user enter a number which represents a line in the text file and then return the text that is in that line.. dose anyone know a way of doing that? is it possible?


Most likely the problem is the file path since you’re not specifying a full path name. When you want to access a file inside your application bundle, use -[NSBundle pathForResource:ofType]:

NSString *path = [[NSBundle mainBundle] pathForResource:@"nCode01"
                                                 ofType:@"txt"];
nCode = [NSFileHandle fileHandleForReadingAtPath:path];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜