ifstream and binary file reading problem....

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;

}
 
R

REH

sredd01 said:
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);
This is implementation-specific. The size of i and j are as least 2, but
may not be big enough to hold four bytes.
 
V

Victor Bazarov

sredd01 said:
Please look the code below where I am reading the first 2,2,4 bytes
from a binary file using two methods.

Both methods are non-standard. You should consider posting to a newsgroup
dedicated to your platform, since those are probably platform-specific, or
to a newsgroup for your compiler in case they are compiler-specific.
I am getting a wierd (wrong) output with ifstream and memcpy method,

Perhaps you shouldn't use memcpy...
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);

I am unaware of any 'attach' members in 'ifstream'. Why don't you simply
open the stream instead of this?

myStream.open("my_binary_file.inf", ios::binary);
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);

This is rather strange. Why don't you simply read the fields are they are
intended (and in order to compare to the CFile method):

myStream.read(reinterpret_cast<char*>(&i), sizeof(short));

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

}

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top