Extracting 2 byte decimal values from a binary file
Hi, I am trying to extract to byte decimal values from a binary file for anaylsis.
my current code
Code:
#include "../../std_lib_facilities.h"
using namespace std;
int main()
{
vector<double>xCount;
vector<double>yCount;
vector<double>header;
vector<double>sample;
string searchPattern;
double max;
double min;
double keyMax;
double finalTime;
char * memblock =0;
int size;
ifstream file("c:\\WAV\\BEandDAexp4_22post.wav",ios::ate|ios::binary);
if(file.is_open())
{
int data;
cout<< "Starting Stream" << endl;
size = (int)file.tellg(); // Use's eof operator to see how large the file is.
data = (size-44);
memblock = new char[data];
file.seekg(44,ios::beg);
file.read(memblock, data); // Reads file using 'size' as the itterator of where to stop, set by ios::ate
max =0;
min =0;
int conv = 0;
for( int i =0; i<data ; ++i ) //For loop that will store the values of the binary vector as double's
{ //by casting and storing in private data member yCount
conv = static_cast<double>(memblock[i]);
yCount.push_back(conv);
if(conv>max)
{ // starting with zero and comparing to our string
max = conv; // Finds new max value and time associated with it.
}
else if (conv<min)
{ // starting with zero and comparing to our string finds largest negative value
min = conv;
}
}
delete []memblock;
}
for(int i=0; i<200; i+=2)
{
cout<<static_cast<int>((yCount[i]) << " ";
}
file.close();
Out puts the correct single byte decimal values.
I need them in two byte pieces because the sample sizes are two bytes but, the problem is,
.read() that I am using takes a char * as it's argument.
Re: Extracting 2 byte decimal values from a binary file
I am confused. You say 2 byte decimal, but you seem to want
a double (which is probably 8 bytes). Plus "decimal" is not a C++
type. Do you mean a short ? unsigned short ? both of which
might be 2 bytes in size.
If so, the basic algorithm would be:
Code:
short value; // unsigned short ???
while ( file.read( (char *)&value , sizeof(short) ) )
{
// do something with value
}
Bookmarks