Question

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:@"nCode01.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?

Was it helpful?

Solution

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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top