On Aug 18, 2008, at 8:00 PM, David wrote:

I know it sounds like a basic question, but I've looked and it doesn't seem
as obvious to me as it should.
I want to write out data to a file to describe my tree structure.

I'd like to have a method which appends a string to the end of a text file.

I don't really want to store everything in memory and write out one big
string because it might get quite large.

How are you supposed to do this in Cocoa? Why isn't it more obvious?

I've looked in NSString, NSData, NSFileHandle, NSOutputStream, but I don't see a simple way to append a string to a file. Can it really be this hard?

NSString doesn't have a method to append to a file, nor a stream, nor
NSData.
And the other classes don't have a method to accept a string.

You were pretty close to a solution, I think. Here's how I'd do it (typed in Mail, may not compile, etc.):

NSString *string = @"string";
NSFileHandle *fh = [NSFileHandle fileHandleForWritingAtPath:pathToFile];
[fh seekToEndOfFile];
[fh writeData:[string dataUsingEncoding:NSUTF8StringEncoding]];
[fh closeFile];

You could easily wrap this up in an NSString category if needed.

--
Adam

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

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 [EMAIL PROTECTED]

Reply via email to