[comp.sys.mac.hypercard] opening files from an XCMD

tim@hoptoad.uucp (Tim Maroney) (04/21/89)

In article <7929@june.cs.washington.edu> moore@june.cs.washington.edu (Charles Moore) writes:
>Is there any problem with accessing files from an XCMD?  I'm trying to
>write an XCMD that opens and reads from an existing file and creates and
>writes to another file.  Substantially similar code works fine as a 
>stand-alone application but my attempts at opening either of these files
>from within my XCMD always fail.  Does anybody know why this might be?
>I'm writing my XCMD in Lightspeed C and am using the standard C file I/O
>functions fopen(), fscanf(), and fprintf().

The standard C I/O system requires access to its globals and very
likely to run-time code that is executed when your application starts
up.  One or both are missing in an XCMD environment.  Use the Macintosh
File Manager calls instead; moving from standard C I/O, you will
probably find the high-level calls more appealing.  However, you will
be unable to use fscanf and fprintf.  sscanf and sprintf may work
instead, and you can then easily bring their buffers to or from the
file with FSRead and FSWrite.

If sscanf and sprintf don't work, there are a few things you can do.
First, most things done with scanf and printf are not all that hard to
do yourself with fairly simple code.  If you do have a need to automate
the printf functionality rather than formatting the strings yourself,
you may wish to look into the Mac trap Munger.
-- 
Tim Maroney, Consultant, Eclectic Software, sun!hoptoad!tim
"As I was walking among the fires of Hell, delighted with the enjoyments of
 Genius; which to Angels look like torment and insanity.  I collected some of
 their Proverbs..." - Blake, "The Marriage of Heaven and Hell"

dierks@ndmath.UUCP (Tim Dierks) (04/22/89)

From article <7057@hoptoad.uucp>, by tim@hoptoad.uucp (Tim Maroney):
> In article <7929@june.cs.washington.edu> moore@june.cs.washington.edu (Charles Moore) writes:
>>Is there any problem with accessing files from an XCMD?  I'm trying to
>>write an XCMD that opens and reads from an existing file and creates and
>>writes to another file.  Substantially similar code works fine as a 
>>stand-alone application but my attempts at opening either of these files
>>from within my XCMD always fail.  Does anybody know why this might be?
>>I'm writing my XCMD in Lightspeed C and am using the standard C file I/O
>>functions fopen(), fscanf(), and fprintf().
> The standard C I/O system requires access to its globals and very
[...]
> be unable to use fscanf and fprintf.  sscanf and sprintf may work
> instead, and you can then easily bring their buffers to or from the
> file with FSRead and FSWrite.

Please correct me if I'm wrong, but I believe that all you have to do to use
any of LSC's libraries in a code resource (anything that doesn't have
its globals hanging off of A5) by opening up the project you want to include
(stdio, whatever) changing its type to anyting but Application, and recompiling
all the files.  Now its globals hang off of A4.  If you set up A4 correctly
(as described in the manual) you're set.  For more information, see pages
84-5 of your LSC manual (version 3.0).


Tim Dierks
dierks@darwin.cc.nd.edu