From: essam@gagme.chi.il.us (Essam Khairullah)
Subject: Creating files (where did it go?)
Originator: @gagme
Organization: Gagme Public Access UNIX, Chicago, Illinois.
Date: Mon, 12 Oct 1992 17:15:48 GMT


I have been using a Preferences file for an application I'm writing.  Until now,
I had been hard-wiring the name of the Preferences folder and building a partial
filename, and using that with the System Folder's vRefNum (from SysEnvirons)
with FSOpen (and Create when the file doesn't exist...) to open my prefs file.

Of course, the 'official' Apple Thought Police say that this is a Bad Thing.
So, I decided to use the officially sancioned FindFolder to locate the Prefs
folder.  Of course, this returns a directory ID (not a working directory ID), 
so I have to use the PBH---- series of calls to open/create the file.

Everything goes just peachy keen, with no OSErrors being reported by any of the
routines I call.  The file gets created and everything, but after my program
quits, it disappears!  I can't find it anywhere!  And every time the program
runs, it has to create the file again!  I can't figure it out!

If anyone has any kind of idea what might be going on, please let me know...

Enclosed is my source code:

OSErr	openPrefsFile( short *prefsFileRef)
	{
	Str255			tempStr;
	short			foundVRefNum;
	long			foundDirID;
	OSErr			theErrID;
	HParamBlockRec	pb;
	
	GetIndString( tempStr, miscStringID, prefsFileNameID);
	theErrID = FindPrefsFolder( &foundVRefNum, &foundDirID);
	
	pb.ioParam.ioNamePtr = (StringPtr) &tempStr;
	pb.ioParam.ioPermssn = fsRdWrPerm;
	pb.ioParam.ioMisc = 0;				/*	Use volume i/o buffer	*/
	pb.ioParam.ioVRefNum = foundVRefNum;
	pb.fileParam.ioDirID = foundDirID;
	
	theErrID = PBHOpen( &pb, FALSE);
	*prefsFileRef = pb.ioParam.ioRefNum;
	
	if ( theErrID == fnfErr)
		{
		theErrID = PBHCreate( &pb, FALSE);
		if (!theErrID)
			{
			pb.fileParam.ioFlFndrInfo.fdType = 'pref';
			pb.fileParam.ioFlFndrInfo.fdCreator = 'FlBk';
			theErrID = PBHSetFInfo( &pb, FALSE);
			}
		if (!theErrID)
			{
			theErrID = PBHOpen( &pb, FALSE);
			*prefsFileRef = pb.ioParam.ioRefNum;
			}
		}
	if (!theErrID)
		{
		theErrID = SetFPos( *prefsFileRef, fsFromStart, NIL);
		}
	return( theErrID);
	}


Basically this just opens (or creates and then opens) the preferences file and
returns the fileRefNum in prefsFileRef.  The next routine, FindPrefsFolder
actually locates the Preferences folder and returns the directory ID and volume
reference number.  If there is not Preferences folder, it returns info on the
active System Folder.  This was copied almost verbatim from a similar routine 
that I got off of the Developer's CD (May 1992; The Byte Stuff) by Mac DTS.


OSErr	FindPrefsFolder(short *foundVRefNum, long *foundDirID)
	{
	long			gesResponse;
	SysEnvRec		envRec;
	WDPBRec			myWDPB;
	unsigned char	volName[34];
	OSErr			err;
	
	
	*foundVRefNum = 0;
	*foundDirID = 0;
	if (!Gestalt (gestaltFindFolderAttr, &gesResponse) &&
		BTstQ (gesResponse, gestaltFindFolderPresent))
		{	/* Does Folder Manager exist? */
			err = FindFolder (kOnSystemDisk, kPreferencesFolderType, kDontCreateFolder, 
				foundVRefNum, foundDirID);
		}
	else
		{
		/* Gestalt can't give us the answer, so we resort to SysEnvirons */
		if (!(err = SysEnvirons (curSysEnvVers, &envRec))) {
			myWDPB.ioVRefNum = envRec.sysVRefNum;
			volName[0] = '\000';					/* Zero volume name */
			myWDPB.ioNamePtr = volName;
			myWDPB.ioWDIndex = 0;
			myWDPB.ioWDProcID = 0;
			if (!(err = PBGetWDInfo (&myWDPB, 0)))
				{
				*foundVRefNum = myWDPB.ioWDVRefNum;
				*foundDirID = myWDPB.ioWDDirID;
				}
			}
		}
	return (err);
	}


That's it!  Please let me know if you can untangle this mess!

-- 
                    Essam Badi' Abu Khalil Khairullah
			  essam@gagme.chi.il.us
	             I don't got no fancy signature!
I'm a .signature virus. You can't resist copying me into your .signature!