Re: File I/O
Re: File I/O
- Subject: Re: File I/O
- From: Jason Coco <email@hidden>
- Date: Fri, 19 Sep 2008 17:29:45 -0400
On Sep 19, 2008, at 17:20 , Nick Zitzmann wrote:
On Sep 19, 2008, at 3:15 PM, Jordon Hirshon wrote:
How can I read a file a line at a time (i.e. getline)? I'm trying
to do this in a Cocoa Framework.
Try using NSFileHandle to read a file until a line feed is
encountered. There's no built-in method of stopping at a character,
but you could always read it in byte by byte.
Or you could read the file into a string and split it with \n,
although Nick's method is probably more efficient:
NSError *error;
// use the proper encoding if it's not UTF-8
NSString *string = [[NSString alloc] initWithContentsOfFile:path
encoding:NSUTF8StringEncoding error:&error];
if( !string ) { /* do something with the error */ }
NSArray *lines = [[string componentsSeparatedByString:@"\n"]
retain]; // assuming line terminator is \n for this file
[string release];
for( NSString *line in lines ) {
// process each line
}
[lines release];
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
References: | |
| >File I/O (From: Jordon Hirshon <email@hidden>) |
| >Re: File I/O (From: Nick Zitzmann <email@hidden>) |