S
sredd01
Hello,
Please look the code below where I am reading the first 2,2,4 bytes
from a binary file using two methods.
I am getting a wierd (wrong) output with ifstream and memcpy method,
but get the correct output with CFile read.
I am trying to get the values 1400 1050 and 2014 from the binary file.
Can anybody point out the mistake in the following code?
int main ()
{
ifstream myStream;
filedesc fd;
char sz2[30];
short i = 0;
short j = 0;
//Start Method one
fd = _open("my_binary_file.inf", _O_RDONLY, _O_BINARY);
myStream.clear();
myStream.attach(fd);
myStream.read(sz2, 20);
memcpy( &i, &sz2[0], 2 );
printf("x value is %d \n", i);
memcpy( &i, &sz2[2], 2 );
printf("y value is is %d \n", i);
memcpy( &i, &sz2[4], 4 );
printf("Version is %d \n", i);
myStream.close();
myStream.clear(); // reset eof state
//End Method one
//Start Method two
CFile file("my_binary_file.inf", CFile::modeRead);
file.Read((LPSTR)&j, 2);
printf("x value is %d \n", j);
file.Read((LPSTR)&j, 2);
printf("y value is %d \n", j);
file.Read((LPSTR)&j, 4);
printf("Version is %d \n", j);
file.Close();
//End Method two
return 0;
}
Please look the code below where I am reading the first 2,2,4 bytes
from a binary file using two methods.
I am getting a wierd (wrong) output with ifstream and memcpy method,
but get the correct output with CFile read.
I am trying to get the values 1400 1050 and 2014 from the binary file.
Can anybody point out the mistake in the following code?
int main ()
{
ifstream myStream;
filedesc fd;
char sz2[30];
short i = 0;
short j = 0;
//Start Method one
fd = _open("my_binary_file.inf", _O_RDONLY, _O_BINARY);
myStream.clear();
myStream.attach(fd);
myStream.read(sz2, 20);
memcpy( &i, &sz2[0], 2 );
printf("x value is %d \n", i);
memcpy( &i, &sz2[2], 2 );
printf("y value is is %d \n", i);
memcpy( &i, &sz2[4], 4 );
printf("Version is %d \n", i);
myStream.close();
myStream.clear(); // reset eof state
//End Method one
//Start Method two
CFile file("my_binary_file.inf", CFile::modeRead);
file.Read((LPSTR)&j, 2);
printf("x value is %d \n", j);
file.Read((LPSTR)&j, 2);
printf("y value is %d \n", j);
file.Read((LPSTR)&j, 4);
printf("Version is %d \n", j);
file.Close();
//End Method two
return 0;
}