OpenRF
OpenRF Open the resource fork of an existing file
#include <Files.h> File Manager
OSErr OpenRF( fileName, vRefNum, fRefNum );
Str255 fileName ; address of length-prefixed full or partial name
short vRefNum ; volume or directory reference number
short *fRefNum ; receives file reference number
returns Error Code; 0=no error
OpenRF opens the resource fork of an existing file. Resources are normally
accessed via the Resource Manager functions such as LoadResource,
GetResource, GetCursor, etc., which manage the resource map and other
details automatically. About the only reason to use OpenRF is to perform a
file-copy operation.
fileName is the address of a length-prefixed, pascal-style string containing
the name of the file to be opened. It may be a partial or full
pathname, depending upon the value of vRefNum .
vRefNum is the reference number of the volume or directory containing
fileName . See FSOpen for the various options.
fRefNum is the address of a short. Upon return, it will contain the file
reference number (also called the access path number) of the file's
resource fork. This value is used in all subsequent operations on the
open file.
Returns: an operating system Error Code. It will be one of:
noErr (0) No error
bdNamErr (-37) Bad name
extFSErr (-58) External file system
ioErr (-36) I/O error
nsvErr (-35) No such volume
opWrErr (-49) File already open with for writing
tmfoErr (-42) Too many files open

Notes: The following program duplicates both forks of a file named "MyFile" in
the default directory to the name "CopyOfMyFile". You can also use the
System 7 call FSpExchangeFiles.
Example
#include <Files.h>
/* This program copies both forks of a particular file. */
main()
{
short srcRef, destRef;
FInfo fi; /* FInfo struct gets creator, type */
quitErr( GetFInfo( "\pHD 20:MyFile", 0, &fi ) );
/* Be sure to call Create before calling CreateResFile. Although
CreateResFile will create both forks of the file, it will fail silently if
the file already exists and has a non-empty resource fork. An alternative
is to call ResError after calling CreateResFile to make sure that the
call worked
*/
quitErr( Create( "\pCopyOfMyFile", 0, fi.fdCreator, fi.fdType ) );
CreateResFile( "\pHD 20:MyFile" );
quitErr( FSOpen( "\pHD 20:MyFile", 0, &srcRef ) );
quitErr( FSOpen( "\pHD 20:CopyOfMyFile", 0, &destRef ) );
CopyFile( srcRef, destRef );
quitErr( FSClose( srcRef ) );
quitErr( FSClose( destRef ) );
quitErr( OpenRF( "\pmyfile", 0, &srcRef ) );
quitErr( OpenRF( "\pCopyOfMyFile", 0, &destRef ) );
CopyFile( srcRef, destRef );
quitErr( FSClose( srcRef ) );
quitErr( FSClose( destRef ) );
}
/* --- function copies all bytes from one open file to another --- */
CopyFile( short src, short dest )
/* src, dest = file reference numbers */
{
long curPos, srcSize, count;
short err;
char theBuf[512];
quitErr( GetEOF( src, &srcSize ) );
if ( srcSize == 0 )
return(0);
while( TRUE ) {
quitErr( GetFPos( src, &curPos ) );
if ( curPos >= srcSize ) break; /* exit when past EOF */
count = 512;
err = FSRead( src, & count, theBuf );
if ( err != eofErr ) quitErr( err ); /* ok to go past EOF */
quitErr( FSWrite( dest, & count, theBuf ) ); /* same count */
}
SetEOF( dest, srcSize ); [TOKEN:12074] adjust to correct end-of-file */
}
/* --- Error 'checking'; quits on any non-zero value --- */
quitErr( short errCode )
{
if ( errCode == 0 )
return(0);
printf("Error %d\n", errCode );
}