473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

byte reading problem

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 charin("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.

Apr 25 '07 #1
2 2137
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 charin("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();

--
OU
Apr 25 '07 #2
On Apr 25, 4:37 pm, sri <srinivasarao_mot...@yahoo.comwrote:
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 charin("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.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 26 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Steve Mauldin | last post by:
I came across an example in the MSDN documentation using RC2 encryption(the link to the article is at the end of this message). When I tried it I had a problem with getting back the same length...
4
by: Gernot Frisch | last post by:
Hi, when porint apps to powerpc the byte order get's swapped to low-endian. Now, what problems can occur and what should I take care of when porting to mac e.g.? -- -Gernot int main(int...
2
by: Sathyaish | last post by:
I am using MCI (winmm.dll) to read, record and playback sound. For now, I am doing this with disk files instead of realtime doing it straight from the memory. If I want to stream/relay/transmit...
8
by: Eik | last post by:
Hey, I got a serieus problem with reading a 26-byte (0x1A, 032, b00011010: SUB ) from a file. Take this example: ---BEGIN PROGRAM--- #include <stdio.h>
4
by: Luis | last post by:
Hi, I can a big problem... because I have a byte data = new byte, and I need to save this to a field on my MySQL database (here I have a BLOB field) but ¿can I save it? because if I put: Insert...
3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
2
by: Li Zhang | last post by:
I have a problem with reading byte from a memory stream. Basically I want to resize a image and then store them to sql server. Here is part of the codes. every time I tried to read byte from...
4
by: joey.powell | last post by:
Hello, I need to be able to read a binary file that contains fields written in BOTH big endian and little endian byte orders. I am currently unable to read the big endian fields (erroneous...
5
by: moni | last post by:
Hey, My buffer contains a short int, some char, and a structure in form of a byte array. Read the string as: TextBox4.Text = System.Text.Encoding.ASCII.GetString(buffer1, 0, 31); Read...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.