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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Read Problem between Windows and Redhat Linux

Status
Not open for further replies.

sajiv77

Programmer
Jan 2, 2002
14
IN
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);

}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top