Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I parse a text file in Objective-C?

Tags:

iphone

I need to parse a text file, one line at a time. Also, is there EOF in Objective-C?

like image 473
satish Avatar asked Aug 24 '09 04:08

satish


People also ask

How do I read a text file in Objective C?

Firstly, the contents of a file may be read and stored in an NSData object through the use of the contentsAtPath method: NSFileManager *filemgr; NSData *databuffer; filemgr = [NSFileManager defaultManager]; databuffer = [filemgr contentsAtPath: @"/tmp/myfile. txt" ];


2 Answers

Something like this might work for you:

NSString *fileContents = [NSString stringWithContentsOfFile:@"myfile.txt"];
NSArray *lines = [fileContents componentsSeparatedByString:@"\n"];

This will give you an array where each element is a line of the string.

like image 153
rein Avatar answered Nov 09 '22 04:11

rein


Objective-C is a proper extension of C. Any C program is a valid Objective-C program. Among other things, this means that EOF defined in the standard C header "stdio.h" is an EOF marker in Objective-C as well.

like image 29
Stephen Canon Avatar answered Nov 09 '22 04:11

Stephen Canon