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

Trying to figure out how to read a data stream

P: n/a
I am new to C++ and have a data file I want to read, which was stored
in binary. I have looked at the data with a hex editor and it appears
to be correct. Whenever I try to read it though as an integer it
returns the 8bit integer values. I can convert these to hex and they
match up with what I am seeing in the data file. I guess what I need
to do somehow buffer these inputs and then read them as floats, but I
am not sure how to do this.
<br>
Is there a beginning/intermediate C++ book that covers reading binary
data and then translating it to another type?

Jan 8 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Jan 8, 11:32 pm, phwashing...@comcast.net wrote:
I am new to C++ and have a data file I want to read, which was stored
in binary. I have looked at the data with a hex editor and it appears
to be correct. Whenever I try to read it though as an integer it
returns the 8bit integer values. I can convert these to hex and they
match up with what I am seeing in the data file. I guess what I need
to do somehow buffer these inputs and then read them as floats, but I
am not sure how to do this.
<br>
Is there a beginning/intermediate C++ book that covers reading binary
data and then translating it to another type?
How do you read the data? If you only get the 8-bit values it might be
that you are only reading 8-bits at a time. Show us some code and we
might help you. As for book I know none that deals specifically with
your problem but Google is your friend, should be lots of examples out
there.

--
Erik Wikström

Jan 9 '07 #2

P: n/a

Erik Wikström wrote:
On Jan 8, 11:32 pm, phwashing...@comcast.net wrote:
I am new to C++ and have a data file I want to read, which was stored
in binary. I have looked at the data with a hex editor and it appears
to be correct. Whenever I try to read it though as an integer it
returns the 8bit integer values. I can convert these to hex and they
match up with what I am seeing in the data file. I guess what I need
to do somehow buffer these inputs and then read them as floats, but I
am not sure how to do this.
<br>
Is there a beginning/intermediate C++ book that covers reading binary
data and then translating it to another type?

How do you read the data? If you only get the 8-bit values it might be
that you are only reading 8-bits at a time. Show us some code and we
might help you. As for book I know none that deals specifically with
your problem but Google is your friend, should be lots of examples out
there.

--
Erik Wikström
I think that part of my problem with not being successful with google
is that I don't know that much about C++

Following is the code that I have used and gotten the correct values
for the 8bit integers. From what I have read I need to create a class
and buffer this to 32 bit and translate it to float. I found some old
C++ books which appear to do this, but I have not been able to get
those programs to compile.

#include <iostream>
#include <fstream>
#include <cstdlib>

int main( )
{
const int unsigned DATA_SIZE = 100;
long int data_array[DATA_SIZE];
std::ifstream data_file("firstdataB.dat");
int unsigned i;

if (data_file.bad( )) {
std::cerr << "Error: Could not open numbers.dat";
exit(8);
}

for (i = 0; i < DATA_SIZE; ++i) {
assert(i >= 0);
assert(i < sizeof(data_array)/sizeof(data_array[0]));

data_file >data_array[i];
}

float total; //Total of the numbers

total = 0;
for (i = 0; i < DATA_SIZE; ++i) {
assert(i >= 0);
assert(i < sizeof(data_array)/sizeof(data_array[0]));
std::cout << " Number = " << data_array[i] << '\n';
//total += data_array[i];
}
//std::cout << "Total of all the number is " << total << '\n';
return (0);
}
~

Jan 10 '07 #3

P: n/a
On Jan 10, 2:49 am, phwashing...@comcast.net wrote:
Erik Wikström wrote:
On Jan 8, 11:32 pm, phwashing...@comcast.net wrote:
I am new to C++ and have a data file I want to read, which was stored
in binary. I have looked at the data with a hex editor and it appears
to be correct. Whenever I try to read it though as an integer it
returns the 8bit integer values. I can convert these to hex and they
match up with what I am seeing in the data file. I guess what I need
to do somehow buffer these inputs and then read them as floats, but I
am not sure how to do this.
<br>
Is there a beginning/intermediate C++ book that covers reading binary
data and then translating it to another type?
How do you read the data? If you only get the 8-bit values it might be
that you are only reading 8-bits at a time. Show us some code and we
might help you. As for book I know none that deals specifically with
your problem but Google is your friend, should be lots of examples out
there.

I think that part of my problem with not being successful with google
is that I don't know that much about C++

Following is the code that I have used and gotten the correct values
for the 8bit integers. From what I have read I need to create a class
and buffer this to 32 bit and translate it to float. I found some old
C++ books which appear to do this, but I have not been able to get
those programs to compile.
Ok, so what you have is a file in which you have 32-bit float values
stored and you want to read them?
>
#include <iostream>
#include <fstream>
#include <cstdlib>

int main( )
{
const int unsigned DATA_SIZE = 100;
Is this the number of floats or the number of bytes? If bytes that's
equal to 25 floats and you could just as well use 'const unsigned int
DATA_SIZE = 25;'.
long int data_array[DATA_SIZE];
Again, if floats use 'float data_array[DATA_SIZE];', if not you must be
aware that the size of long is not necessarily the same as the size of
float.
std::ifstream data_file("firstdataB.dat");
int unsigned i;
You can skip this line and declare the loop-variable in the for-loop,
see below.
>
if (data_file.bad( )) {
std::cerr << "Error: Could not open numbers.dat";
exit(8);
}

for (i = 0; i < DATA_SIZE; ++i) {
You can use 'for (int i = 0; i < DATA_SIZE; ++i) {' instead, or
unsigned int if you want.
assert(i >= 0);
This is not necessary if you have an unsigned int since it can never be
less than 0.
assert(i < sizeof(data_array)/sizeof(data_array[0]));
Not sure what you are checking here, but what you ought to be looking
for is if the fstream is still OK, if you reach the end of the file
before you are done looping or some other error you need to check this
before the next line.
>
data_file >data_array[i];
If you declare data_array as float as I shown above you will read in
the next 4 bytes as a float, if you don't you'll need to use
reinterpret_cast later.
}

float total; //Total of the numbers

total = 0;
You can make that 'float total = 0;' and save one line :-)
for (i = 0; i < DATA_SIZE; ++i) {
assert(i >= 0);
assert(i < sizeof(data_array)/sizeof(data_array[0]));
See comment about the above loop and asserts.
std::cout << " Number = " << data_array[i] << '\n';
//total += data_array[i];
If you don't declare the array as float you'll have to replace
'data_array[i]' with 'reinterpret_cast<float>(data_array[i])' but as I
pointed out above, long and float are not guaranteed to have the same
size so it's not safe.
}
//std::cout << "Total of all the number is " << total << '\n';
return (0);
}
I hope I understood you correctly, if not; try again.

--
Erik Wikström

Jan 10 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.