首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用NSFileHandle打开文本文件

如何用NSFileHandle打开文本文件
EN

Stack Overflow用户
提问于 2011-06-15 06:36:43
回答 1查看 2.3K关注 0票数 0

我正在尝试打开一个小文本文件,以测试该文件上的一些NSFileHandle函数。然而,我不知道该怎么做,如果你能告诉我我错过了什么,那就太好了。

//.h

代码语言:javascript
复制
@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

代码语言:javascript
复制
- (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“……我做错了什么?

从这里我希望找到一种方法,让用户输入一个数字,代表文本文件中的一行,然后返回该行中的文本。有谁知道这样做的方法吗?有可能吗?

EN

Stack Overflow用户

回答已采纳

发布于 2011-06-15 06:56:28

最有可能的问题是文件路径,因为您没有指定完整的路径名。当您想要访问应用程序包中的文件时,请使用-[NSBundle pathForResource:ofType]

代码语言:javascript
复制
NSString *path = [[NSBundle mainBundle] pathForResource:@"nCode01"
                                                 ofType:@"txt"];
nCode = [NSFileHandle fileHandleForReadingAtPath:path];
票数 3
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6350988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档