Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read a text file in ios

Tags:

ios

iphone

I am reading a txt file with a very simple code. The problem is that in the Log statments I only get the path of the file but contents are null. I guess that the problem lies in encoding part.Any Suggestions on how to read my text file.

NSStringEncoding encoding;
NSString* content;
NSString* path = [[NSBundle mainBundle] pathForResource:@"colorpalette" ofType:@"txt"];
if(path)
{                                               
content = [NSString stringWithContentsOfFile:path  usedEncoding:&encoding  error:NULL];
}
NSLog(@"path is %@",path);
if (content)
{
    NSLog(@" content of file is %@",content);
}
like image 326
Shubham Avatar asked Feb 08 '12 08:02

Shubham


1 Answers

Here encoding parameter is not set properly.

Try this

NSString* content = [NSString stringWithContentsOfFile:path
                                              encoding:NSUTF8StringEncoding
                                                 error:NULL];
NSLog(@"%@",path);
like image 56
iOSPawan Avatar answered Sep 30 '22 19:09

iOSPawan