Trying to figure out how to read a data stream

P

phwashington

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?
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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.
 
P

phwashington

Erik said:
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.

#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;
}

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 << '\n';
//total += data_array;
}
//std::cout << "Total of all the number is " << total << '\n';
return (0);
}
~
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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;


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 << '\n';
//total += data_array;


If you don't declare the array as float you'll have to replace
'data_array' with 'reinterpret_cast<float>(data_array)' 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.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top