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

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

Here's a tough one.  I have a big c library for munging scientific
data files and putting them out in a standard format.  It uses
the standard c buffered io (fopen, fwrite,fread,fclose).  It compiles
and works fine on the Mac using Think C.
    Now I would like to use it in an application, giving the user the
chance to specify where (s)he wants to save the output file, using
the standard SFPutFile dialog.  The problem is, this returns volid
and so forth designed to work with FSOpen, FSRead, etc, and not the
file pointer that c wants.  So I can't figure out how to set the
volume/folder I'm writing to.  I can use Create to make a mac file
in the folder I want, using the output of SFPutFile, but the c
fopen(...) call seems to always set the volume/folder to the one
from which the application is launched (as far as I can tell).  Is
there any way to get around this?  Given the complexity of the
library I am working with it would be impractical to go through
and replace all the c io calls with the equivalent Mac File
Manager calls.  
     Does fopen really always set the volume/folder the way I
think?  Is there any kind of SetVol call that would allow
me to specify the volume/folder that fopen will use?  
    Please reply via email to rtp1@midway.uchicago.edu
.

bc@Apple.COM (bill coderre) (11/17/90)

Here is a fragment of Think C that allows Think's <stdio> lib and Mac's
Standard File dialogs to be intermixed:

	/* Open the input file. */
	MyTypes[0] = 'TEXT';
	SFGetFile(SFGWhere,NIL,NIL,1,MyTypes,NIL,&reply);
	if (!reply.good) return;
	SetVol(0L, reply.vRefNum);
	strcpy(inFileName,reply.fName);
	PtoCstr(inFileName);
	inp = fopen(inFileName,"r");
	if (inp == NULL)
	{
		strcpy(&sTemp,"Can't seem to open input file: ");
		strcat(&sTemp,inFileName);
		ParamText(CtoPstr(sTemp),"\p","\p","\p");
		A_Lose();
		return;
	};

Since emacs and apple.com's dialins are fighting the XON/XOFF game, please
excuse any spurious garbage.

The function A_Lose puts up an "uh-oh" alert and effectively aborts the
program. Primitive but functional errorchecking. 

I got this idea and almost all the code itself by calling Think C's tech
support people. Thanks guys.

good luck
bc
private consultant, not apple spokesdroid