RE: [Newbie] writing plain text to a file
RE: [Newbie] writing plain text to a file
- Subject: RE: [Newbie] writing plain text to a file
- From: Kevin Elliott <email@hidden>
- Date: Fri, 28 Feb 2003 15:28:25 -0800
At 04:59 PM -0600 on 2/28/03, Mondragon, Ian wrote:
chris,
here's a cocoa-ized quickie for you, then <grin>.
use fprintf(fp, ...) to print to the file, and use [[obj description]
cString] to get char* representations of objects (cuz the %@ format string
used in NSLog() just calls an object's -description method...which you
should always implement in your own objects <wink>).
Wow that's ugly. No offense, but "description" is intended for
debugging, not as a string conversion function.
There is not need to use fopen, etc. Use NSFileHandle. Convert the
NSString to an NSData object using NSString's "dataUsingEncoding:" or
"dataUsingEncoding:allowLossyConversion:". Then write it out using
NSFileHandle's "write
Data:".
in my code I use the following to write the data out:
- (void) writeString: (NSString*) stringData
{
NSData* tData= [stringData dataUsingEncoding: [NSString
defaultCStringEncoding] allowLossyConversion: YES];
[myFile writeData: tData];
[myFile synchronizeFile];
}
open the file for writing by call:
myFile= [NSFileHandle fileHandleForWritingAtPath: filePath];
close the file by calling "closeFile" or just releasing the file handle.
--- snip ---
- (FILE *)openFile:(NSString *)filename mode:(NSString *)mode
{
FILE *fp;
// man fopen() for modes - add some error checking, too!
if (NULL == (fp = fopen([filename cString]), [mode cString])))
{
return fp;
}
}
- (void)closeFile:(FILE *)fp
{
if (NULL != fp)
{
fclose(fp);
}
}
--- snip ---
-----Original Message-----
From: Chris Garaffa [SMTP:email@hidden]
Sent: Friday, February 28, 2003 4:30 PM
To: email@hidden
Subject: Re: [Newbie] writing plain text to a file
Ian -
On Friday, February 28, 2003, at 04:46 PM, Mondragon, Ian wrote:
> why not just use standard C FILE* pointers? man fopen(), man fclose()?
Well, one, because I don't know C ;) (I come from a C++ background),
and two, I'm really trying to learn Cocoa as well as I can.
I'm going to look into this method, though, because this program has to
be ready to go in a week or so.
--
Chris Garaffa
email@hidden
http://moondrop.heli0s.net/
> - ian
>
>> -----Original Message-----
>> From: Chris Garaffa [SMTP:email@hidden]
>> Sent: Friday, February 28, 2003 3:28 PM
>> To: email@hidden
>> Subject: [Newbie] writing plain text to a file
>>
>> Hi all,
>>
>> I've been programming Cocoa for a couple months now, but getting
>> really
>> into it the last 2 or 3 weeks. I've got this program which generates a
>> random number in a specified interval, and writes that number, and the
>> next 100 or so numbers above it. It needs to write all these numbers
>> to
>> a file, each on its own line. What I do now is create an
>> NSMutableArray
>> to hold all the numbers, and then [numArray writeToFile:
>> numFileLocation atomically:YES]. This works... it creates the file and
>> the data is in there, but I (and my users!) would rather have just a
>> plain text file with one number on each line instead of this XML file.
>> I did some research and found a lot of stuff about NSFileHandle,
>> NSFileManager and NSData but I couldn't figure out the best way to do
>> this. Can someone point me in the right direction? Thanks!
>>
>> --
>> Chris Garaffa
>> email@hidden
>> http://moondrop.heli0s.net/
>> _______________________________________________
>> cocoa-dev mailing list | email@hidden
>> Help/Unsubscribe/Archives:
>> http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>> Do not post admin requests to the list. They will be ignored.
> _______________________________________________
> cocoa-dev mailing list | email@hidden
> Help/Unsubscribe/Archives:
> http://www.lists.apple.com/mailman/listinfo/cocoa-dev
> Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
--
__________________________________________
Kevin Elliott <
mailto:email@hidden>
ICQ#23758827 AIM ID: teargo
__________________________________________
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.