Subject: Re: Reading one line at a time using NSFileHandle



On Wed, Mar 25, 2009 at 11:44 PM, Jeff Laing <jeff.laing@xxxxxxxxxxxxxxx> wrote:
>>> Is there a way to read one line of a text file at a time
>>> using NSFileHandle (the way fgets does)?
>>
>> This code illustrates how you might approach
>> filtering lines of data obtained through an NSFileHandle.
>
> I've come across this problem as well - its fine to say "read it all
> into memory, then split it into lines seperated by my arbitrary
> delimiter", but that doesn't work in small-memory environments (like the
> iPhone) for big files which would otherwise be fine to parse line by
> line.
>
> Answers of the form "...but how do you know what the delimiter character
> is, Unicode, blah blah" aren't all that helpful. ÂLets assume that when
> someone says fgets() they *know* that they want to access an 8-bit
> ascii, newline-delimited text file line by line.

Note that "8-bit ascii" is an oxymoron. ASCII is a 7-bit encoding by
definition. However the good news is that nearly any encoding of
interest (UTF-16 being a notable exception) will preserve characters
like \n and \r and will not produce those bytes to indicate other
meanings. Treating the file as a raw bag of bytes and parsing for \r
or \n will work for virtually every situation.

> So far, the only answer I've ever seen is to fall back on
> fopen()/fgets()

Certainly that's a good way to go. It's also relatively easy to do
your own buffering using NSFileHandle or the like and essentially
implement your own line-reading scheme that way.

Mike
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@xxxxxxxxxxxxxxx)

Please do not post admin requests or moder...

ator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/maillists%40codeha.us

This email sent to maillists@xxxxxxxxx



Privacy