Description
This article is from the Mac Programming
FAQ, by Jon Watte h+@austin.metrowerks.com with numerous
contributions by others.
4.1) How do I tell fopen() to open a file the user has selected using StandardGetFile? (Files - Mac Programming)
The "standard" ANSI C file functions are less than well suited for
the Macintosh way of doing things. However, if you are doing a port for
your own enjoyment and benefit (or maybe for in-house work) you can use
the following function: (see below about converting a wdRefNum into a
vRefNum/parID pair)
*code*
FILE *
fopen_mac ( short vRefNum , long parID , char * fileName , char * mode ) {
short oldVol ;
short aVol ;
long aDir , aProc ;
FILE * ret = NULL ;
if ( GetVol ( NULL , & oldVol ) ) {
return NULL ;
}
if ( GetWDInfo ( oldVol , & aVol , & aDir , & aProc ) ) {
return NULL ;
}
if ( HSetVol ( NULL , vRefNum , parID ) ) {
return NULL ;
}
ret = fopen ( fileName , mode ) ;
if ( HSetVol ( NULL, aVol , aDir ) ) {
/* an error we can't currently handle */
}
if ( SetVol ( NULL, oldVol ) ) {
/* an error we can't currently handle */
}
return ret ;
}
*end*
All of the above is necessary for one reason or another - if you are
interested, by all means look HSetVol up in Think Reference 2.0 or New
Inside Mac: Files.
In older versions of MPW; this wouldn't work since the MPW libraries
used to do a GetVol and explicitly use that value by itself.
 
Continue to:
Share and Enjoy
Bookmark this story so others can enjoy it:
Tags
os, Mac, macintosh, apple, programming, software