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

Linux and Windows file read from C

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);

}

 
Hi,

Fairly simple question - if you transferred by ftp did you use type binary for the transfer ? Text files on linux / unix have only a x'0A' as indication of a line-end whereas dos / windows uses two chars - i.e. x'0D0A' . It could be that if you transfered as 'text' then binary values in the original file were zapped by the ftp program doing such a conversion.

The other thing is are these both intel architecture machines ?

Regards


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top