我正在尝试获取包含超过 47,000 个单词(字典)的文件的内容。我的目标是生成一个随机数并将单词定位在文件的特定行,以便在每次运行程序时获得不同的单词,然后输出该单词。我一直在研究但我没有找到任何答案,这就是我目前所拥有的,它只需要字符,但我想要单词
//
// main.m
// wordaday
//
// Created by Eddy Guzman on 11/5/13.
// Copyright (c) 2013 Eddy Guzman. All rights reserved.
//
#import <Foundation/Foundation.h>
bool checkFile( NSString * path)
{
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: path ] == YES)
{
return TRUE;
NSLog (@"File exists");
}
else
{
NSLog (@"File not found");
return false;
}
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
NSString * path = @"/Users/eddy30/Eddy's Documents/School/Millersville/Fall2013/wordaday/dictionary.txt";
if(checkFile(path) == TRUE)
{
NSLog(@"WOHOOOO");
}
NSString* content = [NSString stringWithContentsOfFile:path];
//NSFileHandle *myFile = fileHandleForReadingAtPath:path;
int rand = arc4random_uniform(47049);
char Word = [content characterAtIndex:rand];
NSLog(@"Word of the day is: %c", Word);
}
return 0;
}