lundi 13 septembre 2021

How to read a .raw binary file in c++?

I am trying to read a binary file in .raw format. But when I dereference the pointer, I am getting the garbage value. When I read the file using this code, I can read the file normally. However, it is an expensive operation.

I need to store the data on the heap from the binary file.

You can find the binary file here

Here is the code I tried:

int main(){ 
       std::string file{"input.raw"};

    
    /** first version of reading the binary file */ 
     
       std::ifstream myData(file, std::ios::binary);
       int  buffersize{1280*720*4};
       std::unique_ptr<char>buffer;
       buffer.reset(new char[buffersize]);
        
       myData.read(buffer.get(), buffersize);

       std::cout<<*(buffer.get())<<std::endl;  //getting garbage value

       
    /** second version of reading the binary file */ 
  
       std::unique_ptr<uint8_t>buffer1;
       buffer.reset(new uint8_t[buffersize]);
        
       myData.read(reinterpret_cast<char*>(buffer1.get()), buffersize);

       std::cout<<*(buffer1.get())<<std::endl; //getting garbage value
}

Any help?

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire