我試圖開發一個iOS應用程序,允許用戶輸入一個單詞,然後按下按鈕,得到了這個詞的定義從Objective-C中的文件讀取數據,如何?
其實我有一個包含單詞及其定義,使得
文件Apple , the definition
orange , the definition
我寫的代碼,但我不知道爲什麼它不工作
#import "ViewController.h"
NSString *readLineAsNSString(FILE *file);
@interface ViewController()
@end
@implementation ViewController
@synthesize text;
@synthesize lab;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)butt:(id)sender {
FILE *file = fopen("1.txt", "r");
NSString *a = text.text ;
bool found =FALSE;
while(!feof(file)|| !found)
{
NSString *line = readLineAsNSString(file);
if ((a= [[line componentsSeparatedByString:@","] objectAtIndex:1])) {
lab.text = [[line componentsSeparatedByString:@","] objectAtIndex:0];
}
fclose(file);
}
}
@end
誰能幫我弄清楚什麼是錯的?
你在哪裏存儲'1.txt'? – 2013-03-25 20:21:40
這已經被回答了,但是你不需要在Objective-C中使用'FILE * file':http://stackoverflow.com/questions/1492335/read-text-file-programmatically-using-objective-c – 2013-03-25 20:23:15
在包含頭文件,主文件等的項目文件夾中 – 2013-03-25 20:26:07