By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,303 Members | 1,517 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,303 IT Pros & Developers. It's quick & easy.

ifstream and binary file reading problem....

P: n/a
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;

}

Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
sredd01 wrote:
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
Jul 23 '05 #2

P: n/a
REH

"sredd01" <rs*****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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.
Jul 23 '05 #3

P: n/a
Thanks a lot for your help.
I figured it out.

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.