ifstream - read - problem

N

Nils Wogatzky

Hi,
I´ve got a problem with the iftream.read method.

I´m reading out a binary file, but I receive wrong values if
values are negative.


m_File.read((char*)&help2,2);

so, help2 is datatype signed int
but if the stored value is -1 help2 is 65535
there´s something wrong with the cast I think,
but I don´t know exactly how to handle it.

I think you now!

thank you

Nils
 
S

Samuele Armondi

Nils Wogatzky said:
Hi,
I´ve got a problem with the iftream.read method.

I´m reading out a binary file, but I receive wrong values if
values are negative.


m_File.read((char*)&help2,2);

so, help2 is datatype signed int
but if the stored value is -1 help2 is 65535
there´s something wrong with the cast I think,
but I don´t know exactly how to handle it.

I think you now!

thank you

Nils
That's because char is only 1 byte in size, whereas int is usually at lest 2
bytes. So when you do the cast, you are losing information and getting
inaccurate results.
HTH,
S. Armondi
 
T

Thomas Matthews

Nils said:
Hi,
I´ve got a problem with the iftream.read method.

I´m reading out a binary file, but I receive wrong values if
values are negative.


m_File.read((char*)&help2,2);

so, help2 is datatype signed int
but if the stored value is -1 help2 is 65535
there´s something wrong with the cast I think,
but I don´t know exactly how to handle it.

I think you now!

thank you

Nils

Show more context around your problem.
Perhaps you want:
int help2;
ifstream m_File("my_data.bin", ios_base::binary)

m_file((char *)&help2, sizeof(help2));

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

Nils Wogatzky escribió:
m_File.read((char*)&help2,2);

so, help2 is datatype signed int
but if the stored value is -1 help2 is 65535
there´s something wrong with the cast I think,
but I don´t know exactly how to handle it.

You probably are using a machine where sizeof int is not 2.

Regards.
 
K

Kevin Goodsell

Nils said:
Hi,
I´ve got a problem with the iftream.read method.

I´m reading out a binary file, but I receive wrong values if
values are negative.


m_File.read((char*)&help2,2);

so, help2 is datatype signed int
but if the stored value is -1 help2 is 65535
there´s something wrong with the cast I think,
but I don´t know exactly how to handle it.

I think you now!

thank you

Nils

The bottom line is that you are attempting to read data the wrong way.
You can only expect a direct read into a particular variable to work if
1) the file just happens to be in exactly the right format for that
variable on your particular implementation and 2) the object is of POD type.

The correct way to read data from a binary file is

1) find out what the format of the data in the file is
2) read the data from the file into something like an unsigned char, or
an array of unsigned char
3) interpret the bytes you've read according to the format you
discovered in 1 and store the resulting values in appropriate objects.

This is almost never as simple as "file.read((char*)&some_obj,
sizeof(some_obj))".

-Kevin
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top