Search code examples
c++binaryfilesreadfileistream

Is there a better option than reinterpret_cast<char*> when reading from std::istream?


I have the following piece of code:

std::istream is;
// ... stream initialization ... 
while(is)
{
  uint32_t next4Bytes = 0;
  is.read(reinterpret_cast<char*>(&next4Bytes), 4);
  if(next4Bytes == 218893066)
  {
    is.seekg(-4, std::ios_base::cur);
    break;
  }
  else
    is.seekg(-3, std::ios_base::cur);
}

Is there any other better way than reinterpret_cast<char*> to read 4 bytes from std::istream into a uint32_t? (obviously other than c-style cast)


Solution

  • I don't think there is, and I don't think you need one. You're taking four bytes and re-interpreting them; reinterpret_cast precisely describes your intent.