byte reading problem

S

sri

I think the following question is related to this group that is why I
am posting

The question is:

I am reading bytes from binary file. At a particular position the byte
value is "FF".
I am reading the byte as follows :

std::basic_ifstream<unsigned char> in("somefile.bin",std::ios::in |
std::ios::binary);
unsigned int wData = 0;
unsigned long wPosition = 0x1C8L;
in.seekg(wPosition,std::ios::beg);

//the following statement gives wData value is zero instead "FF"
//read operation is failing
in.read(reinterpret_cast<unsigned char*>(&wData),1);

//but following statement gives wData value is "FF"
in.read(reinterpret_cast<char*>(&wData),1);

what is going wrong?

With Regards,
Sri.
 
O

Obnoxious User

sri skrev:
I think the following question is related to this group that is why I
am posting

The question is:

I am reading bytes from binary file. At a particular position the byte
value is "FF".
I am reading the byte as follows :

std::basic_ifstream<unsigned char> in("somefile.bin",std::ios::in |
std::ios::binary);
unsigned int wData = 0;
unsigned long wPosition = 0x1C8L;
in.seekg(wPosition,std::ios::beg);

//the following statement gives wData value is zero instead "FF"
//read operation is failing
in.read(reinterpret_cast<unsigned char*>(&wData),1);

//but following statement gives wData value is "FF"
in.read(reinterpret_cast<char*>(&wData),1);

wData = in.get();
 
J

James Kanze

I think the following question is related to this group that is why I
am posting
The question is:
I am reading bytes from binary file. At a particular position the byte
value is "FF".
I am reading the byte as follows :
std::basic_ifstream<unsigned char> in("somefile.bin",std::ios::in |
std::ios::binary);

Note that this may or may not compile, and if it compiles, the
behavior of the stream will be implementation defined. For all
intents and purposes, you should forget that the templates
exist, and just use istream or wistream.
unsigned int wData = 0;
unsigned long wPosition = 0x1C8L;
in.seekg(wPosition,std::ios::beg);
//the following statement gives wData value is zero instead "FF"
//read operation is failing
in.read(reinterpret_cast<unsigned char*>(&wData),1);
//but following statement gives wData value is "FF"
in.read(reinterpret_cast<char*>(&wData),1);
what is going wrong?

Well, if you were using ifstream, both of those read statements
would result in undefined behavior. You can't just read one
byte of an unsigned int, and expect to get anything reasonable.

If you're reading binary data, and want reading a single byte
containing 0xFF to set an unsigned int to this value, just using
istream::get() should work. (You'll probably want to assign the
results to an int first, to check for EOF, and then assign the
int to an unsigned int.)
 

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,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top