Re: How do I send an NSString to a text file?
Re: How do I send an NSString to a text file?
- Subject: Re: How do I send an NSString to a text file?
- From: Matt Ronge <email@hidden>
- Date: Wed, 21 Dec 2005 10:49:22 -0600
Kaydell Leavitt wrote:
Hi,
I need to write a function that takes an NSString and
a way to specify a file and write the contents of the
NSString to a text file, using MacRoman encoding.
Can someone give me an example in Objective-C that
will do this?
It is just plain text and I save whatever is in the
NSString which is all 8 bit MacRoman encoding.
If you are doing this on Mac OS X 10.4, then this will be just what you
need:
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ObjC_classic/Classes/NSString_index.html
writeToFile:atomically:encoding:error:
Writes the contents of the receiver to the file specified by /path/
using the specified encoding.
|- (|BOOL|)|writeToFile:|(|NSString *|)/path/
|atomically:|(|BOOL|)/useAuxiliaryFile/ |encoding:|(|NSStringEncoding
<
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ObjC_classic/TypesAndConstants/FoundationTypes.html#//apple_ref/doc/c_ref/NSStringEncoding>|)/enc/
|error:|(|NSError
<
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ObjC_classic/Classes/NSError.html#//apple_ref/doc/c_ref/NSError>
**|)/error/|
Discussion
If /useAuxiliaryFile/ is |YES
<
http://developer.apple.com/documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html#//apple_ref/doc/c_ref/YES>|,
the receiver is written to an auxiliary file, and then the auxiliary
file is renamed to /path/. If /useAuxiliaryFile/ is |NO
<
http://developer.apple.com/documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html#//apple_ref/doc/c_ref/NO>|,
the receiver is written directly to /path/. The |YES
<
http://developer.apple.com/documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html#//apple_ref/doc/c_ref/YES>|
option guarantees that /path/, if it exists at all, won't be corrupted
even if the system should crash during writing.
If /path/ contains a tilde (~) character, you must expand it with
|stringByExpandingTildeInPath
<
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ObjC_classic/Classes/NSString.html#//apple_ref/occ/instm/NSString/stringByExpandingTildeInPath>|
before invoking this method.
This method returns |YES
<
http://developer.apple.com/documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html#//apple_ref/doc/c_ref/YES>|
if the file is written successfully. If there was a problem writing the
file or with the encoding, returns NO and in /error/ an NSError that
describes the problem. If you are not interested in the error, you may
pass in |NULL| for /error/.
Availability
* Available in Mac OS X v10.4 and later.
--
Matt Ronge
www.theronge.com
_______________________________________________
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