"<- Chameleon ->" <ch******@hotma il.NOSPAM.com> wrote in message
news:bf******** **@nic.grnet.gr ...
Well! Maybe I wrote this word incorrect but its not included im my
e-dictionary!
I have a structure with many int, short etc
I execute this function:
fread(&my_struc t, sizeof(struct), 1, filepointer);
The problem is this:
In different endianess platforms from same file will be loaded different
data in the class.
Is there a method to load whole structure at once without this problem?
Until now my approach is:
load whole structure from file.
Instead of my_struct.integ er_no_1 I use
load_integer(my _struct.integer _no_1)
with load_integer:
--------------------------------
// swap char of an 4 bytes long if machine has "LSB last" storage
//
unsigned long load_integer(un signed long a)
{
const static short sample = 0x1234;
const static char *test = (char*) &sample;
if (test[0] == 0x34) return a;
return (a << 24) + ((a & 0xff00) << 8) + ((a & 0xff0000) >> 8) + (a >>
24); }
-------------------------------
but it is very funny approach....
well! another problem is if long is not 4 bytes, but don't do it so
diffucult!...
Hi,
Here is a example for Little/Big Endian machines/files.
NOT for middle endian machines/files (PDP).
Marco
/*
** This program works ONLY for
** Little-Endian and Big-Endian files/processors
** NOT for Middle-Endian
*/
#include <stdio.h>
typedef enum {
ENDIAN_NO_INFOR MATION,
ENDIAN_LITTLE,
ENDIAN_MIDDLE,
ENDIAN_BIG
} ENDIAN_TYPE;
static int FileWrite(const char*,ENDIAN_TY PE,ENDIAN_TYPE) ;
static int FileRead(const char*,ENDIAN_TY PE,ENDIAN_TYPE) ;
static ENDIAN_TYPE UtilEndianType( void);
static void *ReverseBytesIn Array(void*,siz e_t);
static void DumpArray(const void*,size_t);
int main()
{
ENDIAN_TYPE ProcType,FileTy pe;
char *EndianTypeName[]={"NO Information","L ittle","Middle" ,"Big"};
ProcType=UtilEn dianType();
(void)printf("T his is a %s-Endian processor.\n",E ndianTypeName[ProcType]);
FileType=ENDIAN _LITTLE;
if ( FileWrite("Test .bin",ProcType, FileType)!=0 ) return 1;
FileType=ENDIAN _LITTLE;
if ( FileRead("Test. bin",ProcType,F ileType)!=0 ) return 1;
return 0;
}
static int FileWrite(const char *cFile,ENDIAN_T YPE ProcType,ENDIAN _TYPE
FileType)
{
FILE *File;
short sTest;
long lTest;
float fTest;
double dTest;
if ( (File=fopen(cFi le,"wb"))==NUL L ) return 1;
(void)printf("W riting file '%s'\n",cFile);
sTest=0x1234;
(void)printf("% 04X\n",sTest);
if ( ProcType!=FileT ype ) (void)ReverseBy tesInArray(&sTe st,sizeof(short ));
if ( fwrite(&sTest,s izeof(short),1, File)!=1 ) return 1;
lTest=0x1234567 8L;
(void)printf("% 08X\n",lTest);
if ( ProcType!=FileT ype ) (void)ReverseBy tesInArray(&lTe st,sizeof(long) );
if ( fwrite(&lTest,s izeof(long),1,F ile)!=1 ) return 1;
fTest=63.0F;
(void)printf("% .8f\n",fTest);
if ( ProcType!=FileT ype ) (void)ReverseBy tesInArray(&fTe st,sizeof(float ));
if ( fwrite(&fTest,s izeof(float),1, File)!=1 ) return 1;
dTest=1.2345678 90123456789;
(void)printf("% .18lf\n",dTest) ;
if ( ProcType!=FileT ype ) (void)ReverseBy tesInArray(&dTe st,sizeof(doubl e));
if ( fwrite(&dTest,s izeof(double),1 ,File)!=1 ) return 1;
if ( fclose(File)!=0 ) return 1;
return 0;
}
static int FileRead(const char *cFile,ENDIAN_T YPE ProcType,ENDIAN _TYPE
FileType)
{
FILE *File;
short sTest;
long lTest;
float fTest;
double dTest;
if ( (File=fopen(cFi le,"rb"))==NUL L ) return 1;
(void)printf("R eading file '%s'\n",cFile);
if ( fread(&sTest,si zeof(short),1,F ile)!=1 ) return 1;
DumpArray(&sTes t,sizeof(short) );
if ( ProcType!=FileT ype ) (void)ReverseBy tesInArray(&sTe st,sizeof(short ));
(void)printf("% 04X\n",sTest);
if ( fread(&lTest,si zeof(long),1,Fi le)!=1 ) return 1;
DumpArray(&lTes t,sizeof(long)) ;
if ( ProcType!=FileT ype ) (void)ReverseBy tesInArray(&lTe st,sizeof(long) );
(void)printf("% 08X\n",lTest);
if ( fread(&fTest,si zeof(float),1,F ile)!=1 ) return 1;
DumpArray(&fTes t,sizeof(float) );
if ( ProcType!=FileT ype ) (void)ReverseBy tesInArray(&fTe st,sizeof(float ));
(void)printf("% .8f\n",fTest);
if ( fread(&dTest,si zeof(double),1, File)!=1 ) return 1;
DumpArray(&dTes t,sizeof(double ));
if ( ProcType!=FileT ype ) (void)ReverseBy tesInArray(&dTe st,sizeof(doubl e));
(void)printf("% .18lf\n",dTest) ;
if ( fclose(File)!=0 ) return 1;
return 0;
}
static ENDIAN_TYPE UtilEndianType( )
{
ENDIAN_TYPE EndianType=ENDI AN_NO_INFORMATI ON;
unsigned long Value=0x1234567 8;
unsigned char *cPtr=(unsigned char*)&Value;
if ( *cPtr==0x12 && *(cPtr+1)==0x34 && *(cPtr+2)==0x56 && *(cPtr+3)==0x78 )
EndianType=ENDI AN_BIG;
else if ( *cPtr==0x78 && *(cPtr+1)==0x56 && *(cPtr+2)==0x34 &&
*(cPtr+3)==0x12 )
EndianType=ENDI AN_LITTLE;
else if ( *cPtr==0x34 && *(cPtr+1)==0x12 && *(cPtr+2)==0x78 &&
*(cPtr+3)==0x56 )
EndianType=ENDI AN_MIDDLE;
return EndianType;
}
static void *ReverseBytesIn Array(void *Buffer,size_t Size)
{
unsigned char *cPtr0,*cPtr1,c Tmp;
cPtr0=Buffer;
cPtr1=cPtr0+Siz e;
while ( cPtr0<--cPtr1 ) {
cTmp=*cPtr0;
*cPtr0++=*cPtr1 ;
*cPtr1=cTmp;
}
return Buffer;
}
static void DumpArray(const void *Array,size_t Size)
{
const unsigned char *cPtr=Array;
size_t i;
for ( i=0; i<Size; i++ )
(void)printf("% 02X%c",*cPtr++, (i+1<Size)?' ':',');
return;
}