Re: Need help finding examples of read/writing files in Cocoa
Re: Need help finding examples of read/writing files in Cocoa
- Subject: Re: Need help finding examples of read/writing files in Cocoa
- From: Douglas Davidson <email@hidden>
- Date: Wed, 30 Jan 2002 16:22:06 -0800
On Wednesday, January 30, 2002, at 03:36 PM, Greg Chapel wrote:
More specifically I want to directly read/write to a file format that
isn't
a typical file type (ie. jpeg, txt, etc). The file I am trying to
read/write
to is composed as follows; 3200 byte epcidic header, 400 byte binary
header,
then multiple 240 byte trace headers in combination with trace data.
Most all of the cocoa examples supplied with PB on OS X only show how to
work with files that include the object file wrapper, encoding/decoding
followed by archiving/unarchiving. What I want to be able to do is use
the
standard OpenFilePane UI to browse and select files then use my own
custom
object methods to read and/or write to the selected file ultimately
encoding/decoding this data to/from my own custom object.
For reading something like this I usually use a simple
dataWithContentsOfMappedFile:. One line maps in the file and gives you
an NSData you can pass around, and the -[NSData bytes] method gives you
access to it as an array of bytes. Of course, this is read-only, and
the file has to be small enough to fit in your address space.
For writing, the easy way--for files of moderate size--is to assemble an
NSMutableData or NSData and use writeToFile:atomically:. If you really
need to keep a file open for reading, writing, and seeking, you can use
NSFileHandle; or you can just use the POSIX APIs directly.
Douglas Davidson