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("c:\\abc.dat","wb")==NULL)
{
printf( "The file was not opened\n" );
}
else
{
printf( "The file was opened\n" );
}
T1.one=3;
T1.two=7;
T1.three=1;
T1.four=0;
fwrite(&T1,sizeof(T1),1,fp);
fclose(fp);
printf("This will read the File created by c program"
if( (fp = fopen( "c:\\abc.dat", "r+" )) == NULL )
printf( "The file was not opened\n" );
else
printf( "The file was opened\n" );
fread(&T1,sizeof(T1),1,fp);
printf("One is %u \n",T1.one);
printf("Two is %u\n",T1.two);
printf("Three is %u\n",T1.three);
printf("Four is %u\n",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("This will read the File created by c program"
if( (fp = fopen( "abc.dat", "r+" )) == NULL )
printf( "The file was not opened\n" );
else
printf( "The file was opened\n" );
fread(&T1,sizeof(T1),1,fp);
printf("One is %u \n",T1.one);
printf("Two is %u\n",T1.two);
printf("Three is %u\n",T1.three);
printf("Four is %u\n",T1.four);
fclose(fp);
}
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("c:\\abc.dat","wb")==NULL)
{
printf( "The file was not opened\n" );
}
else
{
printf( "The file was opened\n" );
}
T1.one=3;
T1.two=7;
T1.three=1;
T1.four=0;
fwrite(&T1,sizeof(T1),1,fp);
fclose(fp);
printf("This will read the File created by c program"
if( (fp = fopen( "c:\\abc.dat", "r+" )) == NULL )
printf( "The file was not opened\n" );
else
printf( "The file was opened\n" );
fread(&T1,sizeof(T1),1,fp);
printf("One is %u \n",T1.one);
printf("Two is %u\n",T1.two);
printf("Three is %u\n",T1.three);
printf("Four is %u\n",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("This will read the File created by c program"
if( (fp = fopen( "abc.dat", "r+" )) == NULL )
printf( "The file was not opened\n" );
else
printf( "The file was opened\n" );
fread(&T1,sizeof(T1),1,fp);
printf("One is %u \n",T1.one);
printf("Two is %u\n",T1.two);
printf("Three is %u\n",T1.three);
printf("Four is %u\n",T1.four);
fclose(fp);
}