[comp.sys.mac] how to get file size

greid@ondine (Glenn Reid) (03/28/88)

What is the appropriate incantation to determine the size of a file?
I have been digging through the File Manager documentation and such,
and all I can come up with is something like this, which doesn't seem
to work:

	FSOpen ( filename, refnum );
	GetEOF ( refnum, &longsize );

I don't see anything else that seems reasonable.  Any hints?

Please reply by mail; thanks a lot for any help.

Glenn Reid
Adobe Systems
..{sun,decwrl}!adobe!greid

steele@unc.cs.unc.edu (Oliver Steele) (03/28/88)

[Mail bounced.  Follow-ups directed to comp.sys.mac.programmer.]

greid@.UUCP (Glenn Reid) writes:
>What is the appropriate incantation to determine the size of a file?
>I have been digging through the File Manager documentation and such,
>and all I can come up with is something like this, which doesn't seem
>to work:
>
>	FSOpen ( filename, refnum );
>	GetEOF ( refnum, &longsize );

try	FSOpen( filename, volnum, &refnum);
where volnum can be 0 if the file's in the application directory or
in the System File, or copied from the SFReply record if you're using
the Standard File Package.  Make sure that filename is a Pascal string,
like "\pfoo" instead of "foo", and if you want to be safe, check for
a non-zero return value from FSOpen() to see if you got an error.

----------------------------------------------------------------------------
Oliver Steele					   ...!uunet!mcnc!unc!steele
							   steele@cs.unc.edu
"I worry about anyone under eighteen who isn't a cynic --
 and anyone over eighteen who is."     -- Spider Robinson

jv0l+@andrew.cmu.edu (Justin Chris Vallon) (04/05/88)

(Glen, sorry I couldn't send directly, but UUCP & I don't interface :-)

There are two ways to get the size of a Mac file:

(1)
Remember that a Macintosh file consists of two forks: your code (using
"high-level" calls) should look like:
    FSOpen(fileName, volRefNum, &refNum);
    GetEOF(refNum, &dataForkSize);
    FSClose(refNum);
    OpenRF(fileName, volRefNum, &refNum);
    GetEOF(refNum, &rsrcForkSize);
    FSClose(refNum);
    fileSize = dataForkSize + rsrcForkSize;
This is easy, but it requires that you open the file twice :-(.

(2)
Use "low-level" routines.  Now, you have to create a parameter block, but
you can get the information using one ($1, %1, 01, 1.0, exp(0), 1e+0) calls:

    ParamBlkRec paramBlock

    paramBlock.ioNamePtr = fileName;
    paramBlock.ioVRefNum = volRefNum;
    paramBlock.ioFVersNum = 0; /* must be 0 under finder & HFS */
    paramBlock.ioFDirIndex = 0; /* 0 means don't index, just get info */

    PBGetFInfo(&paramBlock, 0); /* returns an OsErr */

    fileSize = paramBlock.ioFlLgLen + paramBlock.ioFlRLgLen;

----

Both routines will give you the same result, but the second one is a whole
lot better (1 direct call vs 6 round-about calls)...

-Justin