Re: Reading one line at a time using NSFileHandle
On Thu, Mar 26, 2009 at 5:48 PM, Erg Consultant
> Tried that approach too. Problem with using NSString is that when it comes
> time to write the line out to the new file, I need an NSData and to create
> one, I have to use NSString's getBytes or getCharacters. Those only return
> unicode arrays, which, when I go to write to a text file gives me a '?'
> between each character. My original file is encoded in MacRoman. There is no
> way to specify the encoing for the lines being written out.
> It is absolutely mind-boggling that Apple does not provide a simple, easy API
> for doing this.
What makes you think they don't?
1) Read a file into an NSString: 1 line of code
2) Split an NSString into lines: 1 line of code
3) Process each line and produce a new one: about 2 lines of code for
the overhead, the processing itself is obviously up to you
4) Combine the lines back into an NSString: 1 line of code
5) Write the combined NSString to a file: 1 line of code
Excluding the application-specific stuff, we're talking about six
lines of code. That definitely qualifies as simple and easy in my
This technique won't work for really huge files, because it loads the
entire thing into memory. But at under one kilobyte, your file is many
orders of magnitude away from that limit, so it doesn't matter.
Cocoa-dev mailing list (Cocoa-dev@xxxxxxxxxxxxxxx)
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 maillists@xxxxxxxxx