ANSI C binary file access

ANSI C binary file access

Post by Dylan Tisda » Thu, 30 Apr 1998 04:00:00



Hi,

I'm writing a simple program in ANSI C which is supposed to read in a file
using the standard fread() function. This works fine on the PCs and UNIX
systems I've tested it on, but on my Macs, it misses the entire resource
fork. Since the program is supposed to recreate the files at the other end
(i.e. fwrite() the data into a new file), I need to read the resources,
otherwise the mac files are useless.

If anyone knows how I can get the resource fork read in with ANSI C (or
with as few Toolbox calls as possible), I'd appreciate hearing from you. I

using the latest version of CodeWarrior (although I can't find a compiler
setting).

Dylan.

 
 
 

ANSI C binary file access

Post by MW R » Thu, 30 Apr 1998 04:00:00




>Hi,

>I'm writing a simple program in ANSI C which is supposed to read in a file
>using the standard fread() function. This works fine on the PCs and UNIX
>systems I've tested it on, but on my Macs, it misses the entire resource
>fork. Since the program is supposed to recreate the files at the other end
>(i.e. fwrite() the data into a new file), I need to read the resources,
>otherwise the mac files are useless.

I don't understand this, if you read and write using fread and fwrite the
data file should be OK.

Ron

--
What's new in Pro 3
http://www.metrowerks.com/desktop/pro/

METROWERKS                   Ron Liechty


 
 
 

ANSI C binary file access

Post by William Edward Wood » Thu, 30 Apr 1998 04:00:00





> >Hi,

> >I'm writing a simple program in ANSI C which is supposed to read in a file
> >using the standard fread() function. This works fine on the PCs and UNIX
> >systems I've tested it on, but on my Macs, it misses the entire resource

                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Quote:> >fork. Since the program is supposed to recreate the files at the other end
   ^^^^
> >(i.e. fwrite() the data into a new file), I need to read the resources,
> >otherwise the mac files are useless.

> I don't understand this, if you read and write using fread and fwrite the
> data file should be OK.

On the Macintosh, the resource fork is separate from the data fork. ANSI
routines fopen(), etc., only operate on the data fork. (Think of the
resource fork and the data fork as two separate files under the same
file name. The resource fork contains a collection of objects (resources),
while the data fork is the traditional PC/UNIX byte stream.)

If you wish to copy a Macintosh file, resources and all, you must use
the OS routines FSpOpenRF(), FSRead(), etc., to open, manipulate, and
copy the resource fork. Of course this means that if you are transfering
files across a network stream you will have to come up with some sort
of protocol specifying where the data fork ends and the resource fork
begins; MacBinary is one such (standard) protocol.

If you wish to completely recreate the file completely, including all
of the finder attributes (such as the creator/type, finder bits, etc),
you will have to use other MacOS routines to get this data. (The
equivalent to 'finder bits' does not exist on the PC or UNIX; these
are invisible data fields that are maintained in the directory along
with the file name that specifies things like who owns the file, and
what color it is under the Finder.)

Hope this helps.

                                        - Bill

--
William Edward Woody                 | In Phase Consulting

http://www.alumni.caltech.edu/~woody | http://www.pandawave.com/

 
 
 

ANSI C binary file access

Post by Ben Trumbu » Fri, 01 May 1998 04:00:00






> >Hi,

> >I'm writing a simple program in ANSI C which is supposed to read in a file
> >using the standard fread() function. This works fine on the PCs and UNIX
> >systems I've tested it on, but on my Macs, it misses the entire resource
> >fork. Since the program is supposed to recreate the files at the other end
> >(i.e. fwrite() the data into a new file), I need to read the resources,
> >otherwise the mac files are useless.

> I don't understand this, if you read and write using fread and fwrite the
> data file should be OK.

Ron, time to grab some coffee :)

You'll need to open and copy the resource fork separately.  fread and
fwrite won't work with resource forks.  Probably the easiest way for you
to adjust a program using fread is to #include <path2fss.h> (in MSL C:MSL
Mac folder).  Then call

OSErr __path2fss(const char * pathName, FSSpecPtr spec);

Take the FSSpecPtr and pass it to FSpOpenRF (a file manager call).  Then
call FSRead, FSWrite, and FSClose, as appropriate.

Alternatively, you could just download the free "MoreFiles" package and
build that source.  Then call its FSpFileCopy function with the FSSpecPtr
and you're all done.  MoreFiles has all the source, so you can see how
they copy the resource fork.  It also has its own path2fss function.

terminally curious,

Ben
___________________________________________________________________
Benjamin Trumbull

Yale University
       You can't be in hell; you can still read your e-mail

 
 
 

ANSI C binary file access

Post by MW R » Fri, 01 May 1998 04:00:00





>> I don't understand this, if you read and write using fread and fwrite the
>> data file should be OK.
>If you wish to copy a Macintosh file, resources and all, you must use
>the OS routines FSpOpenRF(), FSRead(), etc., to open, manipulate, and
>copy the resource fork.

Oh, I misunderstood the question.

Thanks Ben and Woody

Ron

--
What's new in Pro 3
http://www.metrowerks.com/desktop/pro/

METROWERKS                   Ron Liechty

 
 
 

ANSI C binary file access

Post by William Edward Wood » Fri, 01 May 1998 04:00:00



> Oh, I misunderstood the question.

> Thanks Ben and Woody

No prob. Thing is, I misunderstood the question the first couple of times
around as well...

--
William Edward Woody                 | In Phase Consulting

http://www.alumni.caltech.edu/~woody | http://www.pandawave.com/

 
 
 

ANSI C binary file access

Post by John Danie » Tue, 05 May 1998 04:00:00




>I'm writing a simple program in ANSI C which is supposed to read in a file
>using the standard fread() function. This works fine on the PCs and UNIX
>systems I've tested it on, but on my Macs, it misses the entire resource
>fork. Since the program is supposed to recreate the files at the other end
>(i.e. fwrite() the data into a new file), I need to read the resources,
>otherwise the mac files are useless.

>If anyone knows how I can get the resource fork read in with ANSI C (or
>with as few Toolbox calls as possible), I'd appreciate hearing from you. I

>using the latest version of CodeWarrior (although I can't find a compiler
>setting).

By now I assume you know the reason why you  are having problems. However,
I have a better idea that no one has posted yet. Go into the Mac specific
parts of MSL and find file fopen.c (or it could be open.c) and make a copy
of it. Call it fopen_rsrc.c or something like that. You should be able to
make a few modifications and have it open the resource fork instead of the
data fork. Also, there is a segment about truncating that needs to be
commented out. You don't want to truncate the data fork when you open the
resource fork. Then, you can do something like this:

FILE * in = fopen_rsrc("myfile.dat");

I have done this before and it did work. However, I have since gone to a
better, OO method of my own design (that puts MSL to shame BTW). But I
probably have it on backup. Still, I don't know if I could legally post the
code or give it away since it is based on MSL. But any halfway decent Mac
programmer should be able to make these changes. Just change the FSpOpenDF
to FSpOpenRF, yada, yada, yada.

If I get an OK from MW Ron, I might dig up my old code and post it.

John Daniel

Harmony Software

www.harmonysoftware.com

"Just because you're paranoid doesn't mean they aren't out to get you."

 
 
 

ANSI C binary file access

Post by MW R » Wed, 06 May 1998 04:00:00




>If I get an OK from MW Ron, I might dig up my old code and post it.

You will not get an OK from me, the MSL Sources and headers are copyrighted
and can only be distributed with CodeWarrior.  If you can do a diff that
would be the way to go.

Ron

--
What's new in Pro 3
http://www.metrowerks.com/desktop/pro/

METROWERKS                   Ron Liechty

 
 
 

ANSI C binary file access

Post by Ben Trumbu » Wed, 06 May 1998 04:00:00






> >If I get an OK from MW Ron, I might dig up my old code and post it.

> You will not get an OK from me, the MSL Sources and headers are copyrighted
> and can only be distributed with CodeWarrior.  If you can do a diff that
> would be the way to go.

Guys,

Download "MoreFiles" from your favorite mac file archive.  It's Apple DTS
sample code and it gives you express permission to do anything you want to
it except modify it and still claim it's from Apple.

MoreFiles is great.  It does everything you want, and ten thousand things
you never knew you needed.  It's source code and a metrowerks project.

terminally curious,

Ben
___________________________________________________________________
Benjamin Trumbull

Yale University
       You can't be in hell; you can still read your e-mail

 
 
 

ANSI C binary file access

Post by MW R » Wed, 06 May 1998 04:00:00




>Download "MoreFiles" from your favorite mac file archive.

No need to do that,  just look in the Tools CD : Cool Demos, SDKs, & Tools
: SDKs & Libraries : MoreFiles_1.4.8.sit

I think the point was to make the standard library file functions do Mac
features it is not designed for but may be available on other systems.

Ron

--

METROWERKS                   Ron Liechty