I need to parse a text file, one line at a time. Also, is there EOF 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" ];
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With