CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2010
    Posts
    34

    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.

    Thanks in advance

    Steave

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    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
    }

  3. #3
    Join Date
    May 2010
    Posts
    34

    Re: Extracting 2 byte decimal values from a binary file

    OK I am trying to implement the code, and I am probablly doing something naive.

    Code:
        ifstream file("c:\\WAV\\BEandDAexp4_22post.wav",ios::ate|ios::binary);
        
        if(file.is_open()) 
        {
            short value =1;  // unsigned short ???
    
            cout<< "1" << endl;
            while ( file.read( (char *)&value , sizeof(short) ) )
            {
                sample.push_back(value);
            }
        }
    
        for(int a = 0; a<200 ; ++a)
        {
            cout<< sample[a] << " ";
        }
    
    
    }
    It is throwning an error that says vector subscript out of range

    I need to pushback the values of the binary file into a vector to anazlye the data.

    Thanks

    steave

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Extracting 2 byte decimal values from a binary file

    1) in your open statement you have : ios::ate ... so the stream position is set to the
    end of the file. Therefore, no values will be read.

    2) in your for statement at the end, you are using : 200 ... it should be sample.size()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured