Extracting two byte samples from a binary file

Hi, I am trying to extract to byte decimal values from a binary file for anaylsis.

my current code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#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.

Thanks in advance

Steave
1
2
3
4
5
6
double d;
...
d = *(static_cast <double*> (memblock + (n * sizeof(double)));
//or
double* dmemblock = static_cast <double*> (memblock);
d = dmemblock[n];

For more reading, see http://www.cplusplus.com/forum/beginner/11431/

Hope this helps.
it does I really appreciate the help.
could you possibly implement that in code?
I've already given you three examples of that in code; two here and one in the link.
Topic archived. No new replies allowed.