Re: Appending to text file... best way?
Re: Appending to text file... best way?
- Subject: Re: Appending to text file... best way?
- From: Fritz Anderson <email@hidden>
- Date: Fri, 10 Jun 2005 15:25:37 -0500
On 10 Jun 2005, at 12:59 PM, Dave DeLong wrote:
Hi everyone (again),
HI DAVE!!
I've got a text file that I need to write lots of stuff to (auto-
generating HTML files). I was wondering what the best way to go
about doing this would be.
I've tried NSString's "writeToFile", but it has enormous overhead
and I saw my remaining hard drive space spiral down the further
along it got. It's also not very fast.
I've been looking at NSOutputStream's write method, but I'm not
exactly sure what all "(const uint8_t *)" means or how I would go
about writing an NSString via the write method.
I also found on Cocoadev.com something about using an NSFileHandle,
but the only "write" method I found was one involving NSData, which
isn't what I'm looking for (I think).
You are up against the fact that files are sequences of bytes,
whereas NSStrings are fairly-abstract sequences of characters.
NSString is something you use in-memory for parsing input and
constructing user-readable output. A file is there to store and be
read again, and is reduced to an encoding. There are many encodings,
and not all agree what code stands for the Icelandic capital eth, or
the Japanese Kanji "ken," or even whether a code exists for them.
So before you cross from memory-and-data-structure land to file-and-
coding land, you have to choose a coding. This turns your
presentable, navigable, parseable NSString into a monolithic blob o'
bytes, which is handled in Cocoa as an NSData.
So if NSString's writeToFile methods don't suit you, choose an
encoding (NSUTF8StringEncoding is safe) and use dataUsingEncoding: to
produce the corresponding NSData. Write that or use its bytes and
length methods to feed NSOutputStream, as you choose.
-- F
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden