At 1:38 PM +1100 2/3/09, Jacob Rhoden wrote:
On 3/02/2009 8:41 AM, Kenneth Bruno II wrote:
I am wondering what the best way to read a text file, line by line, when the file size is much larger than available memory.
For very large files you probably want to use NSFileHandle. With the method readDataOfLength: you can read the file in chunks and then parse through for the line endings.

I also find the non existence of a readLine type mechanism quite strange. Either way, this has given me enough to go on so thanks for the help!

I can just read in large blocks, read each line within the large block, then use the index of the last line ending to fetch the next large block.

Umm, unless I'm totally missing something, what's wrong with fopen() and fgets(), possibly followed with [NSString stringWithCString] on each line?

fgets() returns the EOL character, so you can detect lines that don't fit in the buffer. I'll leave handling this case as an exercise.

I'm guessing that Cocoa would assume access to the standard C library and doesn't bother duplicating functionality.

At 8:02 PM -0800 2/2/09, Peter Duniho wrote:
For line delimiters, it's almost always going to be \n or \r\n. It's simple enough to treat either as a line-break, or to ask the user if for some reason \n is a valid character within a line of a \r\n-delimited file (but that's very rare). Certainly, line-breaks are a lot easier to deal with in a general, reliable way than character encodings. :)

If you're doing your own line-end detection, I've used the following algorithm:

1) If you see either \r or \n you're at a line end.

2) If the next character is the other, skip it

This handles \r, \n, \r\n, \n\r, plus gracefully handles files with mangled line endings with the only possible failure of eating or emitting a stray blank line at the point were line endings change in the file.

(This thread seems to be off in the weeds)

Cheers,

-Steve


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to