Re: ASCII File IO
Re: ASCII File IO
- Subject: Re: ASCII File IO
- From: Sherm Pendley <email@hidden>
- Date: Wed, 16 Mar 2005 20:40:16 -0500
On Mar 16, 2005, at 5:22 PM, Randy Bradley wrote:
I just worked through the chapter on archiving in Hillegass's book
and I'm amazed at how easy it is to save and load data from my
application. Now I want to explore exporting and importing data to
and from other applications which will typically be stored in text
files.
I'm with you so far...
I take it I want to implement "readFromFile" and "writeToFile".
Why?
Would one want to override readFromFile to create an NSData
"dataWithContentsOfFile" and pass it on to
"loadDataRepresentation:ofType" for files of my type, but use standard
"fopen" and "fgets" if it is a text file?
Again - why? Text is data, just like anything else. The NSData object
you get with loadDataRepresentation:ofType: is just a one-for-one copy
of the bytes in the file. The machinery that loads bytes from a file
doesn't know or care what they represent - a .txt of the Gettysburg
Address, an .mp3 of the Four Seasons, or a .jpg of Miss March are all
handled the same way. There's no reason to treat text as a special
case.
If you want a copy of the data that you can modify, get a mutable copy
of it with -mutableCopy. If you want to use C functions, get a pointer
to the bytes with -bytes. If you want to make it into a string, use
NSString's -initWithData:encoding:.
sherm--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
_______________________________________________
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