在Objective-C中从文本文件读取单行 [英] read single line from text file in objective-C

查看:80
本文介绍了在Objective-C中从文本文件读取单行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是XCode SDK中iPhone编程和编码的新手.我想访问和读取我放置在XCode的Resource文件夹中的配置文件,我的配置文件看起来像这样

i'm new to iPhone programming and coding in XCode SDK.I want to access and read the configuration file which i have placed in Resource folder in XCode,my configuration file looks like this

@key=value$
@vinu=flower$
@cathy=fruit$

我想比较密钥并从配置文件访问值.由于我使用的是iPhone OS,因此无法使用NSWorkspace.因此,我使用的是NSFileHandle.这就是我的代码的样子,

I want to compare the key and access the value from configuration file. Since i'm working with iPhone OS, i cant use NSWorkspace.Hence i'm using NSFileHandle. This is how my code looks like,

NSString *path = [[NSBundle mainBundle] pathForResource:@"configuration" ofType:@"txt"];
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:path];

NSString *txtString = [[NSString alloc] initWithData: 
                                  [readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];

请让我知道该程序是否正确,以及如何进行.??

please let me know is the procedure correct, and how to proceed. ??

谢谢.

推荐答案

帮个忙,将其保存为plist,而不是纯文本文件.

Do yourself a favor and save it as a plist, and not a straight text file.

但是,如果您需要以这种方式读取它,最简单的方法是将其读取为字符串,然后从那里开始,即:

However, if you need to read it in like that, the simplest way is to read it into a string and then go from there, ie:

NSString * fileContents = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

如果文件很大,并且唯一合理的读取方式是逐行读取,那么您很快就会发现这是目标C:逐行读取文件).

If the file is super large and the only reasonable way to read it is line-by-line, then you can quickly see that this is something that has come up before (Particularly: Objective-C: Reading a file line by line).

这篇关于在Objective-C中从文本文件读取单行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆