[comp.sys.mac.programmer] Summary-- SFPutFile with fopen

rtp1@quads.uchicago.edu (raymond thomas pierrehumbert) (11/08/90)

I have received many interesting replies on the question of how
to use the SFReply record (say, theReply) with standard c io if
you have existing code and don't want to replace all the io
with Mac calls.  There were a number of interesting suggestions
for interesting and possible modifications to the code for fopen,
but the solution that worked best for me was one that didn't 
involve any modifications of this sort.  Suppose you
have theReply from calling SFPutFile (or SFGetFile).  Then,
you can open the file you want in the volume you want by just
doing a SetVol, and a PtoCstr conversion.

For an existing file:
char *buf
FILE *myFile
	pStrCopy((macSFReply->fName),buf);PtoCstr(buf);
	SetVol(0L,macSFReply->vRefNum);
	myFile = fopen(buf,"ab");

(This example is for appending binary data to a file).
Here, PStrCopy is a routine for making a copy of a Pascal string
referenced by its pointer (not its stringhandle).

For a new file, everything works the same, except you need
to do a Create first to make the Mac file with the correct
creator type and document type.  Note, that fopen must be
specified with one of the "append" permissions.  "w" and "wb"
seem to wipe out the file you Create'd, and replace it with
a file of its own creation.  Note also, that you need to
keep track of the vRefNum if you ever want to reopen the file
later, since the current volume might have been changed in
the interim.

This all worked nicely, except that for me, the fopens are all in
a big library I don't want to mess with, and so I can't easily
change the access permissions for new files to appends from
"wb".  So, I can make the file where I want, but lose the
creator and type information.  So my
next query:  

Is there an easy way to reset the creator and type of an existing
file ex post facto? I know the answer is in Inside Macintosh, but
then again, the answer to all the questions of the Universe is
in the I Ching, so I could just as well go there.  So what I
need is simple and less obscure advice.
 
(please reply by email to rtp1@midway.uchicago.edu)

peirce@outpost.UUCP (Michael Peirce) (11/08/90)

In article <1990Nov7.182122.6620@midway.uchicago.edu>, rtp1@quads.uchicago.edu (raymond thomas pierrehumbert) writes:
> 
> I have received many interesting replies on the question of how
> to use the SFReply record (say, theReply) with standard c io if
> you have existing code and don't want to replace all the io
> with Mac calls.  There were a number of interesting suggestions
> for interesting and possible modifications to the code for fopen,
> but the solution that worked best for me was one that didn't 
> involve any modifications of this sort.  Suppose you
> have theReply from calling SFPutFile (or SFGetFile).  Then,
> you can open the file you want in the volume you want by just
> doing a SetVol, and a PtoCstr conversion.
> 
> For an existing file:
> char *buf
> FILE *myFile
> 	pStrCopy((macSFReply->fName),buf);PtoCstr(buf);
> 	SetVol(0L,macSFReply->vRefNum);
> 	myFile = fopen(buf,"ab");

There's one really big problem with doing c i/o in a Mac program
and that's error checking and recovery.  If you're using fprintf()
there is no way to get errors back like, disk is full and file is
locked.  The inside mac routines really aren't that hard to use and
allow full error checking.

(I just spent over a week reworking a program for a client of mine
that used c i/o and had massive problems when files were write locked,
vloume locked, disks got full, etc.)

GOOD Mac programs handle these conditions with grace.  If this isn't
for a commercial product, maybe OK, but if this is a commercial product
PLEASE do careful error checking.

> Is there an easy way to reset the creator and type of an existing
> file ex post facto? I know the answer is in Inside Macintosh, but
> then again, the answer to all the questions of the Universe is
> in the I Ching, so I could just as well go there.  So what I
> need is simple and less obscure advice.

Create() works fine for a new file, SetFInfo() work for later changes.

It's well worth it to at least give the File Manager chapter in Volume
IV of Inside Mac a quick read.  You'll understand Mac file i/o a LOT better...

-- michael


--  Michael Peirce         --   {apple,decwrl}!claris!outpost!peirce
--  Peirce Software        --   Suite 301, 719 Hibiscus Place
--  Macintosh Programming  --   San Jose, California 95117
--         and Consulting  --   (408) 244-6554