读取文件到数组 [英] Read file into array

查看:76
本文介绍了读取文件到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有被换行符分隔词/词组的文件。我需要的文件,并读每个词/词组入阵。我有这个至今:

I have a file of words/phrases separated by newlines. I need to get the file and read each word/phrase into the array. I have this so far:

    NSFileHandle *wordsFile = [NSFileHandle fileHandleForReadingAtPath:[[NSBundle     mainBundle] pathForResource:@"WordList"
    																								       ofType:nil]];
    NSData *words = [wordsFile readDataToEndOfFile];
    [wordsFile closeFile];
    [wordsFile release];

但我不知道这是正确的,如果是这样,在那里从那里。

But I'm not sure if that's right, and if so, where to go from there.

而且,teabot的答案

Also, teabot's answer of

的NSString componentsSeparatedByCharactersInSet:NSCharacterSet newlineCharacterSet

NSString componentsSeparatedByCharactersInSet: NSCharacterSet newlineCharacterSet

的伟大工程,但它只有10.5。 如何将这种行为被复制为10.4?

works great, but it's 10.5 only. How would this behavior be replicated for 10.4?

推荐答案

只是为了完整性(因为我无聊)这里是bassed上teabot的后一个完整的例子:

Just for completeness (and because I am bored) here's a complete example bassed on teabot's post:

    NSString *string = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]
        													pathForResource:@"file" ofType:@"txt"]];

    NSArray *array = [string componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    NSLog(@"%@",array);

这篇关于读取文件到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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