Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linux /Windows Non Compatablity on reading files

Status
Not open for further replies.

sajiv77

Programmer
Jan 2, 2002
14
IN
Hi
I have a binary file generated in VC++(Windows) with the fopen and fwrite functions.
Iam able to read the file generated in Windows from Linux with fread but iam getting all wrong values.
I using structures of unsigned char with bit allocation.
Attached is the file code.
First create the file in windows then port it to linux and
read the file.
Can anybody figure out what is the problem.

//Program in Windows.

#include <stdio.h>

typedef struct
{
unsigned char one : 2;
unsigned int two : 3;
unsigned char three : 1;
unsigned char four : 2;
}TEST;

main()
{
FILE *fp;
TEST T1;

if( (fp=fopen(&quot;c:\\abc.dat&quot;,&quot;wb&quot;))==NULL)
{
printf( &quot;The file was not opened\n&quot; );
}
else
{
printf( &quot;The file was opened\n&quot; );
}


T1.one=3;
T1.two=7;
T1.three=1;
T1.four=0;


fwrite(&T1,sizeof(T1),1,fp);
fclose(fp);
printf(&quot;This will read the File created by c program&quot;);

if( (fp = fopen( &quot;c:\\abc.dat&quot;, &quot;r+&quot; )) == NULL )
printf( &quot;The file was not opened\n&quot; );
else
printf( &quot;The file was opened\n&quot; );

fread(&T1,sizeof(T1),1,fp);

printf(&quot;One is %u \n&quot;,T1.one);
printf(&quot;Two is %u\n&quot;,T1.two);
printf(&quot;Three is %u\n&quot;,T1.three);
printf(&quot;Four is %u\n&quot;,T1.four);
fclose(fp);



}


Copy the file abc.dat to linux and then compile the following file and
run it.It will read the file.Compare the windows and linux output.

//Program to be run in Linux to just read the file abc.dat created over

// windows.

#include <stdio.h>

typedef struct
{
unsigned char one : 2;
unsigned int two : 3;
unsigned char three : 1;
unsigned char four : 1;
}TEST;

main()
{
FILE *fp;
TEST T1;

printf(&quot;This will read the File created by c program&quot;);

if( (fp = fopen( &quot;abc.dat&quot;, &quot;r+&quot; )) == NULL )
printf( &quot;The file was not opened\n&quot; );
else
printf( &quot;The file was opened\n&quot; );

fread(&T1,sizeof(T1),1,fp);

printf(&quot;One is %u \n&quot;,T1.one);
printf(&quot;Two is %u\n&quot;,T1.two);
printf(&quot;Three is %u\n&quot;,T1.three);
printf(&quot;Four is %u\n&quot;,T1.four);

fclose(fp);

}

 
Just a thought...

I've had similar problems using HP-UX to windows.

Turns out one system was Little Endian, whilst the other was Big Endian. To fix i just wrote a function to reverse the order of the string i read.


 
The problem is there while declearing the bit field.

If you change the type of the variable within the struct then VC++ will not allocated common memory. If will allocated 12 Bytes for that but Linux will do it with 4 bytes itself.

Do not change the type of the variables, put all are as unsigned int.

you are writing 12 bytes and reading only 4 bytes,becuase ur using the sizeof () in fread & fwrite

Maniraja S


 
Actually the declaration
unsigned int two : 3;
will take only 3 bits for the variable two.Not 12 bytes as u said.It is not 4*3 it is just 3 bits.
I have checked the sizeof unisgned char and unsigned int in both windows and linux.They take 1 and 4 bytes resp..
So whatever is the size iam specifying it should take the same bit assignment .Isnt it.

 
Hi,

Your understanding may be different.
See the sizeof(TEST) in Linx and the same in windows.

Maniraja S
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top