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((ch ar*)&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 5 6309
"Nils Wogatzky" <ni**@wogatzky. com> wrote in message
news:op******** ******@news.t-online.de... 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((ch ar*)&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
Nils Wogatzky escribió: m_File.read((ch ar*)&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.
Samuele Armondi wrote: "Nils Wogatzky" <ni**@wogatzky. com> wrote in message news:op******** ******@news.t-online.de...
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,
True enough (but unrelated to the problem).
whereas int is usually at lest 2 bytes.
Not true.
So when you do the cast, you are losing information and getting inaccurate results.
This is not the problem.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nils Wogatzky wrote: 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((ch ar*)&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((cha r*)&some_obj,
sizeof(some_obj ))".
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: wtnt |
last post by:
Hello.
I've searched all over and haven't seen another thread with this
problem. Please bear with me as I try to explain. thanks. :)
I have some programs that need to be cross-platform compatible (unix
and windowsXP). The first program parses a text file and records where
snippets are in terms of where it begins (char offset from begin of
the file) and length (number of chars).
One can almost use "byte" and "char" interchangeably...
|
by: Ram Laxman |
last post by:
Iam new bie to C++ programming..
I want to write a program which will read the Comma separated
values(CSV) file column wise.
For example:
In a.txt:
"TicketNumber","Phone","CarNumber"
10,20,30
|
by: inkapyrite |
last post by:
Hi all. I'm using ifstream to read from a named pipe but
i've encountered an annoying problem. For some reason, the
program blocks on reading an ifstream's internal buffer that's
only half-filled. Only when the buffer becomes full does
it resume execution.
Here's my test code for reading from a pipe:
//(compiled with g++ -std=c++98)
//---------------------------------------------
#include <iostream>
|
by: sredd01 |
last post by:
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?
|
by: sam |
last post by:
Hi,
Can anyone tell me how to print a file name from ifstream?
the following cout code does not print the filename I created with
ifstream preivous:
ifstream is;
is.open ("text.txt");
| |
by: shawn |
last post by:
Hi All,
Am using MSVC6 compiler on a WinXP machine.
Am trying to read a file using std::ofstream;
the problem is that Tofstream.is_open() fails and Tifstream.rdstate()
returns "2" which translates to "The system cannot find the file
specified.".
This is really weird because the said file am tring to read exists on
the disk at the required location.
SNIPPET:-
|
by: Hamburgpear |
last post by:
Dear All,
Is it possible to reset the value of xxx.peek() after it reachs EOF ?
Regards
HP
|
by: Notebooker |
last post by:
Hello,
I'm an intermediate noob reading-in data from ascii-file using an
ifstream object.
I have specified a c-style string buffer with size of type size_t and I
am specifying to use this buffer size as the number of characters to
read in using the function read(). The issue I am having is read()
expects that the value for the number of characters to read-in will be
of type std::streamsize, which is apparently signed int. My buffer
|
by: toton |
last post by:
Hi,
I want to unread a few things while I am reading a file. One
solution I know is putback the characters read to the buffer.
But as I need frequent moving file pointer , to a few steps back, I
was thinking of seekg & tellg function. But it is not workings as I
expect....
here is the test code,
std::string word;
std::string value;
|
by: Boltar |
last post by:
Hi
I'm using ifstream (which I hardly ever use) to read an ascii text
file (containing numbers delimited by newlines) in a loop using
something like:
ifstream infile("filename")
int value;
infile >value;
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |