Re: How to write a string to a file
Re: How to write a string to a file
- Subject: Re: How to write a string to a file
- From: "Adam R. Maxwell" <email@hidden>
- Date: Mon, 18 Aug 2008 20:09:56 -0700
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 (email@hidden)
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 email@hidden