[comp.sys.mac.programmer] Import Binary Date File from PC

fang@physics.phy.duke.edu (Fang Zhong) (02/06/91)

	I use a PC to automate my experiment. I store my data on the PC in
binary format as:


>	fp = fopen(filename, "ab"); 
>		fwrite(&data, sizeof(float), 1,fp); 
>	fclose(fp);

I can use:

>	fread(&data, sizeof(float), 1, fp);

to read the data back on PC.  I plot my data with Igor on Mac. However, if
I use the same line

>	fread(&data, sizeof(float), 1, fp);

to read the data on Mac after the data has been imported via "default
translation" of Apple File Exchange, I get garbage.  The same is true when
I generate binary data on Mac and read it back on PC.  This problem exits
regardless which mode I use in AFE.

	Can some experts on the network point out what I didn't right?
	Thanks in advance.




--
	Fang Zhong				1-919-684-8247
	Duke University Dept. of Physics	fang@phy.duke.edu
	Durham, N.C.      27706			

boissier@irisa.fr (franck boissiere) (02/06/91)

From article <21958@duke.cs.duke.edu>, by fang@physics.phy.duke.edu (Fang Zhong):
> 
> 	I use a PC to automate my experiment. I store my data on the PC in
> binary format as:
> 
> 
>>	fp = fopen(filename, "ab"); 
>>		fwrite(&data, sizeof(float), 1,fp); 
>>	fclose(fp);
> 
> I can use:
> 
>>	fread(&data, sizeof(float), 1, fp);
> 
> to read the data back on PC.  I plot my data with Igor on Mac. However, if
> I use the same line
> 
>>	fread(&data, sizeof(float), 1, fp);
> 
> to read the data on Mac after the data has been imported via "default
> translation" of Apple File Exchange, I get garbage.  The same is true when
> I generate binary data on Mac and read it back on PC.  This problem exits
> regardless which mode I use in AFE.

AFE only knows about file structured following a format. For text files
it will map end of lines accordingly for Microsoft Word files it will
convert between several other text processing formats.

In your case how to you want AFE to know that this particular file
contains floats written on a PC and convert them into the proper Mac
format?

Your problem is that the byte storage of multibyte data is not the same 
on 80x86 (PC processors) and 680x0 (Mac Processors), the swab() routine
included in most C libraries will do the job for you.



-- 
Franck BOISSIERE                        boissier@irisa.irisa.fr
Prototyping Lab Manager                 boissier@ccettix.UUCP
C.C.E.T.T.   B.P. 59                    boissier%irisa.irisa.fr@uunet.uu.net
35512 CESSON SEVIGNE CEDEX  FRANCE    

egw.weakm@p3.lanl.gov (Eric Wasserman) (02/07/91)

In article <1991Feb6.102550.10982@irisa.fr> boissier@irisa.fr (franck 
boissiere) writes:
> From article <21958@duke.cs.duke.edu>, by fang@physics.phy.duke.edu 
(Fang Zhong):
> > 
> >       I use a PC to automate my experiment. I store my data on the PC 
in
> > binary format as:
> > 
> > 
> >>      fp = fopen(filename, "ab"); 
> >>              fwrite(&data, sizeof(float), 1,fp); 
> >>      fclose(fp);
> > 
> > I can use:
> > 
> >>      fread(&data, sizeof(float), 1, fp);
> > 
> > to read the data back on PC.  I plot my data with Igor on Mac. 
However, if...

Igor version 1.24 includes an XOP called GBLoadWave (in the 'Sample XOPs' 
folder,
 I think) which has an option for swapping bytes which will let you read 
in PC binary
files. This will at least get your data into Igor.



Eric
egw.weakm@p3.lanl.gov

russotto@eng.umd.edu (Matthew T. Russotto) (02/07/91)

In article <21958@duke.cs.duke.edu> fang@physics.phy.duke.edu (Fang Zhong) writes:
>
>	I use a PC to automate my experiment. I store my data on the PC in
>binary format as:
>
>
>>	fp = fopen(filename, "ab"); 
>>		fwrite(&data, sizeof(float), 1,fp); 
>>	fclose(fp);
>
>I can use:
>
>>	fread(&data, sizeof(float), 1, fp);
>
>to read the data back on PC.  I plot my data with Igor on Mac. However, if
>I use the same line
>
>>	fread(&data, sizeof(float), 1, fp);
>
>to read the data on Mac after the data has been imported via "default
>translation" of Apple File Exchange, I get garbage.  The same is true when
>I generate binary data on Mac and read it back on PC.  This problem exits
>regardless which mode I use in AFE.
>
>	Can some experts on the network point out what I didn't right?
>	Thanks in advance.

The floating format of Macs and IBM PCs are simply NOT the same.  Write out
your floats as ASCII values, or find out the float format and write a routine
to convert on the mac.  (it may be simply byte-order reversals)

How do you read this file in IGOR, BTW?
--
Matthew T. Russotto	russotto@eng.umd.edu	russotto@wam.umd.edu
     .sig under construction, like the rest of this campus.

wishart@bnl.gov (Jim Wishart) (02/13/91)

In article <21958@duke.cs.duke.edu> fang@physics.phy.duke.edu (Fang Zhong) 
writes:
>         I use a PC to automate my experiment. I store my data on the PC
> in binary format as:  fwrite(&data, sizeof(float), 1,fp); 
> I can use:  fread(&data, sizeof(float), 1, fp);
> to read the data back on PC.  I plot my data with Igor on Mac. However, 
> if I use the same line       (deleted)
> to read the data on Mac after the data has been imported via "default
> translation" of Apple File Exchange, I get garbage.  The same is true
> when I generate binary data on Mac and read it back on PC.  This problem
 > exits regardless which mode I use in AFE.

The problem is that the byte order is opposite on Intel and Motorola 
microprocessors.  On Intel machines the order is least significant to most 
significant byte, while Motorola is MSB->LSB.

You must write your own translation routines to swap the bytes between 
formats.  I did this in Fortran for a program ported from MS-DOS so that I 
could maintain file compatibility.  I was reading mixed groups of reals, 
short integers and strings in large blocks and swapping the bytes in 
memory.  When it came time to save, the bytes were swapped back to write 
in Intel format.  I wrote one routine to swap bytes and another to swap 
words and applied them as necessary.  Strings were not swapped, shorts 
were byte swapped, and longs were word and byte swapped.


Jim Wishart                wishart@bnl.gov
Chemistry Department
Brookhaven National Laboratory,Upton, NY 11973