从我的文件中获取一些内容到string.how我能得到

时间:2014-11-10 08:23:47

标签: objective-c string

NSString * txtFilePath = [[NSBundle mainBundle] pathForResource:@" /cnuu.rtf" ofType:@" rtf"];

NSString *str5=[NSString stringWithContentsOfFile:txtFilePath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",str5);

2 个答案:

答案 0 :(得分:1)

虽然你的问题不是真的可以理解,但我猜你应该改变@" /cnuu.rtf"到@" cnuu"由于ofType:参数已包含扩展名,因此您没有指定路径,因此您不需要斜杠。

答案 1 :(得分:0)

这是因为您正在阅读/cnuu.rtf.rtf文件,而不是您需要阅读cnuu.rtf

您需要阅读

NSString *txtFilePath = [[NSBundle mainBundle] pathForResource: @"cnuu.rtf" ofType:nil];  

NSString *txtFilePath = [[NSBundle mainBundle] pathForResource: @"cnuu" ofType: @"rtf"];  

并通过

阅读内容
NSString *content = [NSString stringWithContentsOfFile:txtFilePath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@", content);